fctom1215
2020-03-10 4d625b8f7d4eb22209dba53cf19353d8aa7455ea
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
package com.anyun.exam.lib.util;
 
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
 
import com.anyun.exam.lib.RemoteService;
 
import java.util.Locale;
 
public class Speaker {
    private Context context;
    private TextToSpeech tts;
    private SpeakerCallback callback = null;
 
    final public String TAG = Speaker.class.getCanonicalName();
 
    public Speaker(final Context context, final SpeakerCallback cb) {
        // TODO Auto-generated constructor stub
        Log.d(TAG, "Speaker Init...");
        this.context = context;
        this.callback = cb;
 
        tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                Log.d(TAG, "onInit TextToSpeech.SUCCESS");
 
                // TODO Auto-generated method stub
                if (status == TextToSpeech.SUCCESS)
                {
                    Log.d(TAG, "TextToSpeech.SUCCESS");
                    int result = tts.setLanguage(Locale.CHINA);
 
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED)
                    {
                        Log.d(TAG, "TextToSpeech.LANG_NOT_SUPPORTED");
                    }
                    if (callback != null) {
                        callback.PlayInit(true);
                    }
                }
            }
        });
 
        tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String s) {
                if (callback != null) {
                    callback.PlayStart();
                }
            }
 
            @Override
            public void onDone(String s) {
                if (callback != null) {
                    callback.PlayDone();
                }
            }
 
            @Override
            public void onError(String s) {
                if (callback != null) {
                    callback.PlayError();
                }
            }
        });
    }
 
    public void speak(String text) {
//            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        tts.speak(text, TextToSpeech.QUEUE_ADD, null, "speech");
    }
}