MrShi
6 天以前 87294c97b4c071837b9cf1f0e3a31ff13bf3e40b
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.example.nativetts;
 
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import java.util.Locale;
import org.json.JSONObject;
import io.dcloud.feature.expand.unimodule.UniModule;
import io.dcloud.b;
 
public class NativeTTSModule extends UniModule {
    private static final String TAG = "NativeTTS";
    private TextToSpeech mTts = null;
    private boolean isInitialized = false;
    private b moduleContext = null;
    
    public NativeTTSModule(b reactContext) {
        super(reactContext);
        this.moduleContext = reactContext;
        Context context = reactContext.getContext();
        if (context != null) {
            initTTS(context);
        }
    }
    
    private void initTTS(Context context) {
        mTts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int result = mTts.setLanguage(Locale.CHINA);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e(TAG, "TTS 语言不支持");
                        isInitialized = false;
                    } else {
                        isInitialized = true;
                        mTts.setSpeechRate(1.0f);
                        mTts.setPitch(1.0f);
                        Log.d(TAG, "TTS 初始化成功");
                    }
                } else {
                    Log.e(TAG, "TTS 初始化失败");
                    isInitialized = false;
                }
            }
        });
    }
    
    @Override
    public String getModuleName() {
        return "native-tts";
    }
    
    @Override
    public JSONObject exec(JSONObject params, String callbackId) {
        String method = "";
        try {
            method = params.getString("method");
            Log.d(TAG, "exec method: " + method);
            
            if ("init".equals(method)) {
                JSONObject result = new JSONObject();
                result.put("status", isInitialized ? 1 : 0);
                result.put("message", isInitialized ? "初始化成功" : "初始化失败");
                return result;
                
            } else if ("speak".equals(method)) {
                String text = params.getString("text");
                speakText(text, callbackId);
                
            } else if ("stop".equals(method)) {
                stopSpeak();
                JSONObject result = new JSONObject();
                result.put("status", 0);
                result.put("message", "已停止");
                return result;
                
            } else if ("setSpeed".equals(method)) {
                double speed = params.optDouble("speed", 1.0);
                if (mTts != null) {
                    mTts.setSpeechRate((float) speed);
                }
                JSONObject result = new JSONObject();
                result.put("status", 0);
                return result;
                
            } else if ("setPitch".equals(method)) {
                double pitch = params.optDouble("pitch", 1.0);
                if (mTts != null) {
                    mTts.setPitch((float) pitch);
                }
                JSONObject result = new JSONObject();
                result.put("status", 0);
                return result;
                
            } else if ("destroy".equals(method)) {
                destroyTTS();
                JSONObject result = new JSONObject();
                result.put("status", 0);
                result.put("message", "已销毁");
                return result;
                
            } else if ("isReady".equals(method)) {
                JSONObject result = new JSONObject();
                result.put("status", isInitialized ? 1 : 0);
                return result;
                
            } else if ("isSpeaking".equals(method)) {
                JSONObject result = new JSONObject();
                result.put("status", (mTts != null && mTts.isSpeaking()) ? 1 : 0);
                return result;
            }
        } catch (Exception e) {
            Log.e(TAG, "exec error: " + e.getMessage());
        }
        return null;
    }
    
    private void speakText(String text, final String callbackId) {
        if (!isInitialized || mTts == null) {
            try {
                JSONObject result = new JSONObject();
                result.put("status", -1);
                result.put("message", "TTS 未初始化");
                sendCallback(callbackId, result);
            } catch (Exception e) {}
            return;
        }
        
        if (text == null || text.isEmpty()) {
            try {
                JSONObject result = new JSONObject();
                result.put("status", -1);
                result.put("message", "文本不能为空");
                sendCallback(callbackId, result);
            } catch (Exception e) {}
            return;
        }
        
        if (mTts.isSpeaking()) {
            mTts.stop();
        }
        
        mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String utteranceId) {
                Log.d(TAG, "TTS 开始: " + text);
            }
            
            @Override
            public void onDone(String utteranceId) {
                Log.d(TAG, "TTS 完成");
                try {
                    JSONObject result = new JSONObject();
                    result.put("status", 0);
                    result.put("message", "播报完成");
                    sendCallback(callbackId, result);
                } catch (Exception e) {}
            }
            
            @Override
            public void onError(String utteranceId) {
                Log.e(TAG, "TTS 错误");
                try {
                    JSONObject result = new JSONObject();
                    result.put("status", -1);
                    result.put("message", "TTS 播报失败");
                    sendCallback(callbackId, result);
                } catch (Exception e) {}
            }
        });
        
        String utteranceId = "tts_" + System.currentTimeMillis();
        int resultCode = mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null, utteranceId);
        
        if (resultCode == TextToSpeech.ERROR) {
            try {
                JSONObject result = new JSONObject();
                result.put("status", -1);
                result.put("message", "TTS 启动失败");
                sendCallback(callbackId, result);
            } catch (Exception e) {}
        }
    }
    
    private void stopSpeak() {
        if (mTts != null && mTts.isSpeaking()) {
            mTts.stop();
            Log.d(TAG, "TTS 停止");
        }
    }
    
    private void destroyTTS() {
        if (mTts != null) {
            mTts.stop();
            mTts.shutdown();
            mTts = null;
        }
        isInitialized = false;
        Log.d(TAG, "TTS 销毁");
    }
    
    private void sendCallback(String callbackId, JSONObject result) {
        if (callbackId != null && moduleContext != null) {
            try {
                moduleContext.a(callbackId, result);
            } catch (Exception e) {
                Log.e(TAG, "sendCallback error: " + e.getMessage());
            }
        }
    }
}