百度語音合成(TTS)SDK使用方法
阿新 • • 發佈:2019-02-18
語音合成是實現人機語音互動,建立一個有聽和講能力的互動系統所必需的關鍵技術。
這篇文章將介紹如何使用百度Android語音合成SDK。
與語音識別SDK類似,使用語音合成SDK也需要註冊,並開啟語音合成API服務,詳細步驟可以參考 點選開啟連結 中的註冊部分。
百度語音合成SDK以JAR包+動態連結庫形式釋出,需要開發者在 點選開啟連結 下載SDK開發包,並將libs資料夾拷貝到工程中
申請必要的許可權
初始化SpeechSynthesizer物件
引數設定
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
-
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
- <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
初始化SpeechSynthesizer物件
- speechSynthesizer = new SpeechSynthesizer(getApplicationContext(),
- "holder", this);
- // 此處需要將setApiKey方法的兩個引數替換為你在百度開發者中心註冊應用所得到的apiKey和secretKey
-
speechSynthesizer.setApiKey("your-apiKey"
引數設定
- privatevoid setParams() {
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "5");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");
-
speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5"
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE, "1");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE, "4");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_LANGUAGE, "ZH");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_NUM_PRON, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_ENG_PRON, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_PUNC, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_BACKGROUND, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_STYLE, "0");
- speechSynthesizer.setParam(SpeechSynthesizer.PARAM_TERRITORY, "0");
- }
- speechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- class listener implements SpeechSynthesizerListener {
- @Override
- publicvoid onStartWorking(SpeechSynthesizer synthesizer) {
- logDebug("開始工作,請等待資料...");
- }
- @Override
- publicvoid onSpeechStart(SpeechSynthesizer synthesizer) {
- logDebug("朗讀開始");
- }
- @Override
- publicvoid onSpeechResume(SpeechSynthesizer synthesizer) {
- logDebug("朗讀繼續");
- }
- @Override
- publicvoid onSpeechProgressChanged(SpeechSynthesizer synthesizer, int progress) {
- }
- @Override
- publicvoid onSpeechPause(SpeechSynthesizer synthesizer) {
- logDebug("朗讀已暫停");
- }
- @Override
- publicvoid onSpeechFinish(SpeechSynthesizer synthesizer) {
- logDebug("朗讀已停止");
- }
- @Override
- publicvoid onNewDataArrive(SpeechSynthesizer synthesizer, byte[] dataBuffer, int dataLength) {
- logDebug("新的音訊資料:" + dataLength);
- }
- @Override
- publicvoid onError(SpeechSynthesizer synthesizer, SpeechError error) {
- logError("發生錯誤:" + error.errorDescription + "(" + error.errorCode + ")");
- }
- @Override
- publicvoid onCancel(SpeechSynthesizer synthesizer) {
- logDebug("已取消");
- }
- @Override
- publicvoid onBufferProgressChanged(SpeechSynthesizer synthesizer, int progress) {
- }
- }