private SpeechSynthesizer mTts;
private int isSpeaking = 0;
mTts= SpeechSynthesizer.createSynthesizer(this, null);
/*
* 开始合成
*
* @param view
*/
public void start() { mTts.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");
mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速
mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量,范围 0~100
mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
mTts.startSpeaking(search_word_content.getText().toString().trim(), mSynListener);
}
//合成监听器
private SynthesizerListener mSynListener = new SynthesizerListener(){
//缓冲进度回调
//percent为缓冲进度0~100,beginPos为缓冲音频在文本中开始位置,endPos表示缓冲音频在
//文本中结束位置,info为附加信息。
@Override
public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
// TODO Auto-generated method stub }
//会话结束回调接口,没有错误时,error为null
@Override
public void onCompleted(SpeechError arg0) {
// TODO Auto-generated method stub }
//会话事件回调接口
@Override
public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
// TODO Auto-generated method stub }
//开始播放
@Override
public void onSpeakBegin() {
// TODO Auto-generated method stub }
//暂停播放
@Override
public void onSpeakPaused() {
// TODO Auto-generated method stub }
//播放进度回调
//percent为播放进度0~100,beginPos为播放音频在文本中开始位置,endPos表示播放音频在文
//本中结束位置.
@Override
public void onSpeakProgress(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub } //恢复播放回调接口
@Override
public void onSpeakResumed() {
// TODO Auto-generated method stub } };
if (isSpeaking == 0) {
start();
gif_no.setVisibility(View.INVISIBLE);
gif.setVisibility(View.VISIBLE);
isSpeaking = 1;
} else if (isSpeaking == 1) {
// 暂停播放
mTts.pauseSpeaking();
gif_no.setVisibility(View.VISIBLE);
gif.setVisibility(View.INVISIBLE);
isSpeaking = 2;
} else if (isSpeaking == 2) {
// 继续播放
//System.out.println(".........."); mTts.resumeSpeaking();
isSpeaking = 1;
gif_no.setVisibility(View.INVISIBLE);
gif.setVisibility(View.VISIBLE); }
// 取消合成
mTts.stopSpeaking();
isSpeaking = 0;
总结, 开始初始化,start方法里面设置参数,再根据需要调用停止和继续方法,假如换了文本调用取消合成方法,重新start方法