1.用Java實現語音播報,分為四步①音響;②調節語速;③調節音量;④執行對象;⑤執行朗讀;
package net.yuying; /** * 一個用java代碼實現 語音播報示例 * */ import com.jacob.activeX.ActiveXComponent;//如此處報錯,可能是jar的問題 可能要到網上下一個jacob試試 import com.jacob.com.Dispatch;//如此處報錯,可能是jar的問題 可能要到網上下一個 jacob試試 import com.jacob.com.Variant;//如此處報錯,可能是jar的問題 可能要到網上下一個 jacob試試 import java.io.*; public class Test1 { // 程序入口 public static void main(String[] args) { try { voice(); } catch (IOException e) { } } public static void voice() throws IOException { // 拿到音響 ActiveXComponent sap = new ActiveXComponent("sapi.SpVoice"); // 找到本地要朗讀的文件 try { File srcFile = new File("E:/b.txt"); // 獲取文本文檔的內容 FileReader flie = new FileReader(srcFile); // 從緩存區拿到數據 BufferedReader bf = new BufferedReader(flie); // 拿到緩沖區的數據 String content = bf.readLine(); // 測試一下 有沒有拿到數據 System.out.println(content); // 調節語速 音量大小 sap.setProperty("Volume", new Variant(100)); sap.setProperty("Rate", new Variant(0)); Dispatch xj = sap.getObject(); // 執行朗讀 沒有讀完就繼續讀 while (content != null) { Dispatch.call(xj, "Speak", new Variant(content)); } xj.safeRelease(); bf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); sap.safeRelease(); } } }
注意:如果導入前3行錯誤,應該是jar的問題,可能要到網上下一個 jacob安裝可解決!(https://mvnrepository.com/artifact/com.jacob/jacob/1.10)附上下載地址