如何将文字转成语音呢?本文是使用windows自带的语音引擎去实现的。所以需要下载一个jar包。
准备工作:
- 1、jacob-1.17-M2.rar在百度上下载http://download.csdn.net/detail/zzl819748566/9918533
- 2、解压jacob-1.17-M2.rar
- 3、向工程里导入jacob.jar
- 4、将jacob-1.17-M2-x64.dll拷贝到jdk的bin目录和windows/system32目录
public static void main(String[] args) throws IOException {
//
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();
}
}
总结:目前只试过在windows上试过,linux环境下还未用过(未完待续)。其实我是想把文字放到歌曲中的唱出来的,目前还没有一个好的思路。