为什么没有调用UtteranceProgressListener的函数?

时间:2017-03-23 10:06:46

标签: java android google-text-to-speech

当我调用其类的speak函数时,会调用UtteranceProgressListener,但不会调用侦听器的方法,即onStart()onDone()和{ {1}}。

最终,我们想要编写一段代码,可以暂停文本到语音,并使用播放暂停按钮从同一个句子中恢复。

onError()

1 个答案:

答案 0 :(得分:3)

在调用setOnUtteranceProgressListener()之前必须调用speak()方法。现在先调用speakOut()方法,然后再设置监听器。它需要反过来如下:

textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int i) {
        if (i == TextToSpeech.SUCCESS) {
            //set language Locale to US
            int result = textToSpeech.setLanguage(loc);
            //check that is language locale available on device or supported
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(guide.this, "Leanguage not present", Toast.LENGTH_SHORT).show();
            } else {
                textToSpeech.setOnUtteranceProgressListener(
                    new UtteranceProgressListener() {
                    @Override
                    public void onStart(String s) {
                        //if ("id".equals(s)
                        Toast.makeText(guide.this, "onStart works", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onDone(String s) {
                        //if ("id".equals(s))
                        Toast.makeText(guide.this, "onDone works", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(String s) {
                        //if ("id".equals(s))
                        Toast.makeText(guide.this, "OnError works", Toast.LENGTH_SHORT).show();
                    }
                });                            

                speakOut();
            }
        } else {
            //show toast if initialization failed
            Toast.makeText(getBaseContext(), "TTS Engine Initilization Failed!", Toast.LENGTH_SHORT).show();
        }
    }
});