public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MLApplication.getInstance().setAccessToken("your access token");
}
}
3.2 创建TTS配置及TTS 引擎,可以根据需要配置不同参数
MLTtsEngine mlTtsEngine;
MLTtsConfig mlConfigs;
mlConfigs = new MLTtsConfig()
// Setting the language for synthesis.
.setLanguage(MLTtsConstants.TTS_ZH_HANS)
// Set the timbre.
.setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_ZH)
// Set the speech speed. Range: 0.2–4.0 1.0 indicates 1x speed.
.setSpeed(1.0f)
// Set the volume. Range: 0.2–4.0 1.0 indicates 1x volume.
.setVolume(1.0f)
// set the synthesis mode.
.setSynthesizeMode(MLTtsConstants.TTS_ONLINE_MODE);
mlTtsEngine = new MLTtsEngine(mlConfigs);
//Sets the volume of the built-in player.
mlTtsEngine.setPlayerVolume(20);
设置回调(回调见3.3)
// Pass the TTS callback to the TTS engine.
mlTtsEngine.setTtsCallback(callback);
3.3 配置TTS回调,接收处理语音合成的结果
MLTtsCallback callback = new MLTtsCallback() {
String task = "";
String fileName = "audio_" + task;
@Override
public void onError(String taskId, MLTtsError err) {
String str = taskId + " " + err;
sendMsg(str);
}
@Override
public void onWarn(String taskId, MLTtsWarn warn) {
String str = taskId + " 提示:" + warn;
sendMsg(str);
}
@Override
public void onRangeStart(String taskId, int start, int end) {
String str = taskId + " onRangeStart [" + start + "," + end + "]";// + temp.get(taskId).substring(start);
sendMsg(taskId + " onRangeStart[" + start + "," + end + "]");
sendMsg1(taskId, start, end);
}
@Override
public void onAudioAvailable(String taskId, MLTtsAudioFragment audioFragment, int offset,
Pair<Integer, Integer> range, Bundle bundle) {
if (!task.equals(taskId)) {
task = taskId;
fileName = "/sdcard/audio_" + task + ".pcm";
}
writeTxtToFile(audioFragment.getAudioData(), fileName, true);
}
@Override
public void onEvent(String taskId, int eventId, Bundle bundle) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(taskId + " ");
switch (eventId) {
case MLTtsConstants.EVENT_PLAY_START:
stringBuffer.append("onPlayStart ");
break;
case MLTtsConstants.EVENT_PLAY_STOP:
stringBuffer.append("onPlayStop ");
stringBuffer.append(bundle.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED));
break;
case MLTtsConstants.EVENT_PLAY_RESUME:
stringBuffer.append("onPlayResume ");
break;
case MLTtsConstants.EVENT_PLAY_PAUSE:
stringBuffer.append("onPlayPause ");
break;
case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
stringBuffer.append("onSynthesisComplete ");
PCMCovWavUtil.convertWaveFile(fileName);
break;
case MLTtsConstants.EVENT_SYNTHESIS_START:
stringBuffer.append("onSynthesisStart ");
break;
case MLTtsConstants.EVENT_SYNTHESIS_END:
stringBuffer.append("onSynthesisEnd ");
break;
}
Log.d(TAG, "onEvent: " + stringBuffer.toString());
}
};
3.4 调用speak合成请求,及播放控制
String id = mlTtsEngine.speak(text, MLTtsEngine.QUEUE_APPEND));
mlTtsEngine.pause();
mlTtsEngine.resume();
mlTtsEngine.stop();
调用完毕后,释放引擎
if (mlTtsEngine != null) {
mlTtsEngine.stop();
mlTtsEngine.shutdown();
}