Java實現語音朗讀電子書
阿新 • • 發佈:2018-12-26
最近無聊,看小說,又懶得自己看,手機聽書有懶得下載。。就寫了程式碼實現了一下。要實現這個功能,需要jacob-1.17這個架包,配置好dll檔案路徑。
public class ReaderImpl implements ReaderI {
/*** @author 湖居散人
*/
public static String path="E:\\盜墓筆記1-8全集.txt";
static String line;
@Override
public void readIn() throws IOException{
Domain domain = new Domain();
ReaderImpl readerImpl = new ReaderImpl();
FileInputStream fis = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(fis,"GBK");
BufferedReader br = new BufferedReader(isr);
line =domain.line;
//返回有效字元
while((line=br.readLine())!=null){ActiveXComponent sap = new ActiveXComponent("Sapi.SpVoice");
Dispatch sapo = sap.getObject();
try {
// 音量 0-100
sap.setProperty("Volume", new Variant(100));
// 語音朗讀速度 -10 到 +10
sap.setProperty("Rate", new Variant(0));
System.out.println(line);
// 執行朗讀
Dispatch.call(sapo, "Speak", new Variant(line));
} catch (Exception e) {
e.printStackTrace();
} finally {
sapo.safeRelease();
sap.safeRelease();
}
}
br.close();
}
public static void main(String[] args) {
ReaderImpl readerImpl = new ReaderImpl();
try {
readerImpl.readIn();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}