TTSComplexController.java 1.8 KB
Newer Older
T
Takeya Yuki 已提交
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
package yuki.tts.extended;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.webkit.JavascriptInterface;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by Akeno on 2017/08/11.
 */

public class TTSComplexController {
    private Context context;
    private TextToSpeech tts;
    private ArrayList<String> textList;

    public TTSComplexController(final Context context,final Locale locale){

        // TODO Auto-generated constructor stub
        this.context = context;
        textList=new ArrayList<String>();
        tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                // TODO Auto-generated method stub
                if (status == TextToSpeech.SUCCESS) {
                    int result = tts.setLanguage(locale);
                    if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.d("TTS","Language '"+locale.getDisplayLanguage()+"'is not supported");
                    }
                }
            }
        });
        tts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String s) {

            }

            @Override
            public void onDone(String s) {
                if(textList.size()>0) {
                    textList.remove(0);
                }
            }

            @Override
            public void onError(String s) {

            }
        });
    }
    @JavascriptInterface
    public void say(String text) {
        textList.add(text);
        tts.speak(text,TextToSpeech.QUEUE_ADD,null);
    }
}