天天看点

java 文字转换成语音 代码_【Java】将文本转化成语音

【Java】将文本转化成语音

发布时间:2018-05-07 21:45,

浏览次数:540

, 标签:

Java

1.需要的jar包:

2.把dll文件放在%JAVA_HOME%\bin下(注意系统是32位还是64位),也可以放在C:\Windows\System32下,如果是64位应该放在C:\Windows\SysWOW64

3.java代码

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

public class Test {

public static void main(String[] args) { 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(); } }

}