TextToSpeech UtteranceProgressListener不适用于支持Fragment

时间:2019-05-16 10:03:15

标签: java android fragment text-to-speech speech-synthesis

这是代码,其中的片段在run()和done()函数内的Toasts中不起作用。我似乎无法理解问题所在。我尝试传递应用程序以及活动上下文,但没有任何效果

t1 = new TextToSpeech(getContext(), new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int i) {
        if (i == TextToSpeech.SUCCESS) {

            Locale locale = new Locale("en","IN");
            if (t1.isLanguageAvailable(locale) == TextToSpeech.LANG_AVAILABLE) {
                t1.setLanguage(locale);
            } else {
                t1.setLanguage(new Locale("en","IN"));
            }
                t1.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String s) {
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getContext(), "Starrt", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onDone(String s) {
                    getActivity().runOnUiThread(new Runnable() {
                       public void run() {
                           Toast.makeText(getContext(), "Doone", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onError(String s) {
                    Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
});

b1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        t1.speak("Hi user we are glad to meet you. Happy learning", TextToSpeech.QUEUE_FLUSH, null);
    }
});

看着日志猫,我似乎找不到任何信息,除了

2019-05-16 15:27:27.594 21675-21675/com.iaugmentor.ttsdemosj I/ViewRootImpl: jank_removeInvalidNode all the node in jank list is out of time

当我尝试在Activity中运行相同的代码时,敬酒效果很好

0 个答案:

没有答案