JAVA 使用Jacob調用windows的Sapi實現文本轉語音文件

1. 配置Jacob:我這裏使用的jacob爲jacob-1.18,jacob-1.18下載下來以後,解壓縮文件,api

裏面的文件爲docs、jacob.jar、jacob-1.18-x64.dll、jacob-1.18-x86.dll、LICENSE.TXT、README.txt;spa

把jacob.jar複製到項目的lib目錄下面,jacob-1.18-x64.dll和jacob-1.18-x86.dll是兩個動態庫文件,orm

它們指JDK的安裝版本,若是JDK是64位的,那麼須要把jacob-1.18-x64.dll複製到JDK bin目錄下面,由於對象

我裝的JDK是32位的,因此我把jacob-1.18-x86.dll複製到Jdk bin目錄下面,路徑爲"JAVA_HOME"\bin。get

2. 生成的demo代碼以下:io

package com.csg.demo;class

import com.jacob.activeX.ActiveXComponent;test

import com.jacob.com.Dispatch;import

import com.jacob.com.Variant;音頻

public class HelloWord {

public static void main(String[] args) {

ActiveXComponent ax = null;

try {

ax = new ActiveXComponent("Sapi.SpVoice");

Dispatch spVoice = ax.getObject();

ax = new ActiveXComponent("Sapi.SpFileStream");

Dispatch spFileStream = ax.getObject();

ax = new ActiveXComponent("Sapi.SpAudioFormat");

Dispatch spAudioFormat = ax.getObject();

//設置音頻流格式

Dispatch.put(spAudioFormat, "Type", new Variant(22));

//設置文件輸出流格式

Dispatch.putRef(spFileStream, "Format", spAudioFormat);

//調用輸出 文件流打開方法,建立一個.wav文件

Dispatch.call(spFileStream, "Open", new Variant("F:\\test.wav"), new Variant(3), new Variant(true));

//設置聲音對象的音頻輸出流爲輸出文件對象

Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);

//設置音量 0到100

Dispatch.put(spVoice, "Volume", new Variant(100));

//設置朗讀速度

Dispatch.put(spVoice, "Rate", new Variant(-2));

//開始朗讀

Dispatch.call(spVoice, "Speak", new Variant("張三,李四"));

//關閉輸出文件

Dispatch.call(spFileStream, "Close");

Dispatch.putRef(spVoice, "AudioOutputStream", null);

spAudioFormat.safeRelease();

spFileStream.safeRelease();

spVoice.safeRelease();

ax.safeRelease();

} catch (Exception e) {

e.printStackTrace();

}

}

}

相關文章
相關標籤/搜索