How can I Toast after Text to Speech finish speaki

2019-07-09 09:03发布

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条回答
地球回转人心会变
2楼-- · 2019-07-09 10:04

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

查看更多
登录 后发表回答