使用java实现语音朗读

jacob jar包及API下载:https://download.csdn.net/download/hjinping/10391308


1、64位操作系统的将jacob-1.17-M2-x64.dll添加到JDK的bin目录和Windows的system32目录(32位选择对应的dll文件,加入到对应目录下面

2、将jar包加入到项目中

3、编码实现:

demo:

package com.jeeplus.common.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


public class TalkUtil {
public static void main(String[] args) throws Exception {
talkString("你好,很高兴认识你。");
// talkText("C:/aa.txt");


}


public static void talkString(String talk) {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant(talk));
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭应用程序连接
sap.safeRelease();
}
}


public static void talkText(String path) throws Exception {
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// 输入文件
File srcFile = new File(path);
// 使用包装字符流读取文件
BufferedReader br = new BufferedReader(new FileReader(srcFile));
String content = br.readLine();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-1));
// 获取执行对象
Dispatch sapo = sap.getObject();
// 执行朗读
while (content != null) {
Dispatch.call(sapo, "Speak", new Variant(content));
content = br.readLine();
}
// 关闭执行对象
sapo.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
// 关闭应用程序连接
sap.safeRelease();
}
}


}



  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现Java中文转语音实时朗读,可以使用Java Speech API(JSAPI)和FreeTTS库。 以下是一个简单的示例代码,可以实现中文实时转换成语音朗读出来: ```java import java.util.Locale; import javax.speech.Central; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import com.sun.speech.freetts.Voice; import com.sun.speech.freetts.VoiceManager; public class TextToSpeech { public static void main(String[] args) { try { // 初始化JSAPI System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory"); Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral"); Synthesizer synthesizer = Central.createSynthesizer(new SynthesizerModeDesc(Locale.US)); synthesizer.allocate(); synthesizer.resume(); // 初始化FreeTTS Voice voice = VoiceManager.getInstance().getVoice("kevin16"); voice.allocate(); // 中文转语音朗读 String text = "你好,欢迎使用Java语音合成技术。"; synthesizer.speakPlainText(text, null); synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY); voice.speak(text); // 释放资源 voice.deallocate(); synthesizer.deallocate(); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述代码中,我们首先通过JSAPI初始化了语音合成器synthesizer,并通过FreeTTS库获取了中文语音合成的Voice对象voice。然后,我们可以使用synthesizer.speakPlainText()方法将中文转换成语音朗读出来,也可以使用voice.speak()方法实现中文转换成语音朗读出来。 需要注意的是,上述代码需要引入jsapi.jar和freetts.jar两个库文件,可以在FreeTTS官网(http://freetts.sourceforge.net/)下载。另外,由于FreeTTS是基于英语的语音合成库,因此需要将voices属性设置为英语。如果要使用中文语音合成,可以考虑使用其他的语音合成库。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值