Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I Toast after Text to Speech finish speaking Android

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)

like image 901
bigcenima Avatar asked Mar 14 '16 07:03

bigcenima


2 Answers

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

like image 101
0xDEADC0DE Avatar answered Oct 19 '22 22:10

0xDEADC0DE


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);
}
like image 33
kelvin andre Avatar answered Oct 19 '22 23:10

kelvin andre