需要做的工作:
1、下載 jacob-1.17-M2 或 jacob-1.18
2、解壓jacob-1.17-M2 或 jacob-1.18
3、向工程里導入jacob.jar
4、將 jacob-1.18-x64.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(); } }
如果不行:
1.戴上耳機試試;
2.檢查下電腦上是否安裝了office(金山的wps不行)