lmw
2025-01-14 d57f0210ac94ea2b3d4e18f7065d9b06d451ab5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;
    }
}