How can I Toast after Text to Speech finish speaking Android

拟墨画扇 提交于 2019-12-13 15:19:39

问题


How can I Toast after Text to Speech finish speak. Actually I want to do someting more than Log. This is my code.

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {

    private TextToSpeech mTts;
    Button btnSpeak;
    EditText editTextTTS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTts = new TextToSpeech(this,this);
        editTextTTS =(EditText)findViewById(R.id.editText);
        btnSpeak = (Button)findViewById(R.id.btnSpeakTest);
        btnSpeak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speak(editTextTTS.getText().toString());
            }
        });




    }
    private void speak(String word){
        if(word != null) {
            HashMap<String, String> myHashAlarm = new HashMap<String, String>();
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
            myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "Hello");
            mTts.speak(word, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
        }
    }

    @Override
    public void onInit(int status) {
        if(status == TextToSpeech.SUCCESS) {
            mTts.setOnUtteranceCompletedListener(this);
        }
    }

    @Override
    public void onUtteranceCompleted(String utteranceId) {
        Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
        Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
    }

Actually I want to call speech to text after Text to Speech finish speak. How to do something in this method.

03-14 14:35:16.652 5473-5489/com.example.thummawit.testttscallback I/CALLBACK: Hello 03-14 14:35:16.667 5473-5489/com.example.thummawit.testttscallback W/Binder: Caught a RuntimeException from the binder stub implementation. java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:200) at android.os.Handler.(Handler.java:114) at android.widget.Toast$TN.(Toast.java:459) at android.widget.Toast.(Toast.java:120) at android.widget.Toast.makeText(Toast.java:289) at com.example.thummawit.testttscallback.MainActivity.onUtteranceCompleted(MainActivity.java:59) at android.speech.tts.UtteranceProgressListener$1.onDone(UtteranceProgressListener.java:73) at android.speech.tts.TextToSpeech$Connection$1.onSuccess(TextToSpeech.java:2158) at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:63) at android.os.Binder.execTransact(Binder.java:446)


回答1:


You try to show a Toast in a thread that is not the UI(main) thread. You should change this

@Override
public void onUtteranceCompleted(String utteranceId) {
    Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"
    Toast.makeText(getApplicationContext(),"Call     Back",Toast.LENGTH_LONG).show();// I Cannot Toast here. Or do something more than Log
}

into this

@Override
public void onUtteranceCompleted(String utteranceId) {
    Log.i("CALLBACK", utteranceId); //utteranceId == "SOME MESSAGE"

    runOnUiThread(new Runnable() {

        public void run() {
            Toast.makeText(getApplicationContext(),"Call Back",Toast.LENGTH_LONG).show();
        }
    });
}

That way your code is dispatched to the main thread where you are allowed to show Toasts




回答2:


onUtteranceCompleted is deprecated. use OnUtteranceProgressListener

Code Snippet

textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
    @Override
    public void onInit(int status) {
        if (status==TextToSpeech.SUCCESS){
            int result=textToSpeech.setLanguage(Locale.ENGLISH);

            if (result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){
                Log.i("TextToSpeech","Language Not Supported");
            }

            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                @Override
                public void onStart(String utteranceId) {
                    Log.i("TextToSpeech","On Start");
                }

                @Override
                public void onDone(String utteranceId) {
                    Log.i("TextToSpeech","On Done");
                }

                @Override
                public void onError(String utteranceId) {
                    Log.i("TextToSpeech","On Error");
                }
            });

        }else {
            Log.i("TextToSpeech","Initialization Failed");
        }
    }
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null,TextToSpeech.ACTION_TTS_QUEUE_PROCESSING_COMPLETED);
}


来源:https://stackoverflow.com/questions/35982041/how-can-i-toast-after-text-to-speech-finish-speaking-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!