您的位置:首页 > 编程语言 > Java开发

使用JAVA实现语音朗读一段文本

2015-08-20 14:41 1016 查看
最近公司有需求要用JAVA去调用windows自带的语音引擎去朗读一段文字,网上资料少得可怜,把百度和bing都翻遍了,总算找到一段代码,昨天测试的时候,本来应该可以的,结果是耳机坏掉了,今早换了个耳机测试,直接可以了,而且朗读起来还有感情色彩(win7测试),不过多音字还是暂时没解决。

下面是源代码,希望对需要的朋友有帮助。

需要做的工作:

1、jacob-1.17-M2.rar在百度上下载

2、解压jacob-1.17-M2.rar

3、向工程里导入jacob.jar

4、将jacob-1.17-M2-x86.dll拷贝到jdk的bin目录和windows/system32目录

5、源代码如下

public static void main(String[] args) throws IOException {
// ?? 这个Sapi.SpVoice是需要安装什么东西吗,感觉平白无故就来了
ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
// Dispatch是做什么的?
Dispatch sapo = sap.getObject();
try {

// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 语音朗读速度 -10 到 +10
sap.setProperty("Rate", new Variant(-2));

Variant defalutVoice = sap.getProperty("Voice");

Dispatch dispdefaultVoice = defalutVoice.toDispatch();
Variant allVoices = Dispatch.call(sapo, "GetVoices");
Dispatch dispVoices = allVoices.toDispatch();

Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
// 执行朗读
Dispatch.call(sapo, "Speak", new Variant("曾小明小朋友,早上好"));

} catch (Exception e) {
e.printStackTrace();
} finally {
sapo.safeRelease();
sap.safeRelease();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 朗读