package com.xianning.driver.base.gaode.gpsnav.util;
|
|
import android.content.Context;
|
import android.os.Handler;
|
import android.os.Message;
|
|
import com.baidu.navisdk.adapter.IBNTTSManager;
|
|
import java.util.LinkedList;
|
|
/**
|
* 当前DEMO的播报方式是队列模式。其原理就是依次将需要播报的语音放入链表中,播报过程是从头开始依次往后播报。
|
* <p>
|
* 导航SDK原则上是不提供语音播报模块的,如果您觉得此种播报方式不能满足你的需求,请自行优化或改进。
|
*/
|
public class TTSController implements ICallBack {
|
|
//IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_NOT_INIT; //未初始化
|
//IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_IDLE; // 空闲
|
//IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_PLAYING; // 播放中
|
//IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_PAUSE; // 暂停
|
//IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_ERROR; // 错误
|
public int baiduState = IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_NOT_INIT;
|
|
@Override
|
public void onCompleted(int code) {
|
baiduState = IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_IDLE;
|
if (handler != null) {
|
handler.obtainMessage(1).sendToTarget();
|
}
|
}
|
|
public static enum TTSType {
|
/**
|
* 讯飞语音
|
*/
|
IFLYTTS,
|
/**
|
* 系统语音
|
*/
|
SYSTEMTTS;
|
}
|
|
public static TTSController ttsManager;
|
private Context mContext;
|
private TTS tts = null;
|
private SystemTTS systemTTS;
|
private IFlyTTS iflyTTS = null;
|
private LinkedList<String> wordList = new LinkedList<String>();
|
private final int TTS_PLAY = 1;
|
private final int CHECK_TTS_PLAY = 2;
|
private Handler handler = new Handler() {
|
@Override
|
public void handleMessage(Message msg) {
|
super.handleMessage(msg);
|
switch (msg.what) {
|
case TTS_PLAY:
|
if (tts != null && wordList.size() > 0) {
|
tts.playText(wordList.removeFirst());
|
}
|
break;
|
case CHECK_TTS_PLAY:
|
if (!tts.isPlaying()) {
|
handler.obtainMessage(1).sendToTarget();
|
}
|
break;
|
default:
|
}
|
|
}
|
};
|
|
public void setVideoText(String s){
|
try {
|
baiduState = IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_PLAYING;
|
tts.playText(s);
|
}catch (Exception e){
|
|
}
|
}
|
|
public void setVideoTextInNavi(String s){
|
try {
|
if (wordList != null)
|
wordList.addLast(s);
|
handler.obtainMessage(CHECK_TTS_PLAY).sendToTarget();
|
}catch (Exception e){
|
|
}
|
}
|
|
public void setTTSType(TTSType type) {
|
if (type == TTSType.SYSTEMTTS) {
|
tts = systemTTS;
|
} else {
|
tts = iflyTTS;
|
}
|
tts.setCallback(this);
|
}
|
|
private TTSController(Context context) {
|
mContext = context.getApplicationContext();
|
systemTTS = SystemTTS.getInstance(mContext);
|
iflyTTS = IFlyTTS.getInstance(mContext);
|
tts = iflyTTS;
|
}
|
|
public void init() {
|
if (systemTTS != null) {
|
systemTTS.init();
|
}
|
if (iflyTTS != null) {
|
iflyTTS.init();
|
}
|
tts.setCallback(this);
|
baiduState = IBNTTSManager.IBNOuterTTSPlayerCallback.PLAYER_STATE_IDLE;
|
}
|
|
public static TTSController getInstance(Context context) {
|
if (ttsManager == null) {
|
ttsManager = new TTSController(context);
|
}
|
return ttsManager;
|
}
|
|
public void stopSpeaking() {
|
if (systemTTS != null) {
|
systemTTS.stopSpeak();
|
}
|
if (iflyTTS != null) {
|
iflyTTS.stopSpeak();
|
}
|
wordList.clear();
|
}
|
|
public void destroy() {
|
if (systemTTS != null) {
|
systemTTS.destroy();
|
}
|
if (iflyTTS != null) {
|
iflyTTS.destroy();
|
}
|
ttsManager = null;
|
}
|
}
|