Question

I've got a speakText() method as follows

public void speakText(){
        String toSpeak = "Testing 1 2 3";
        Toast.makeText(getApplicationContext(), toSpeak, 
        Toast.LENGTH_SHORT).show();
        mTts.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}

It works fine with a button push (i.e. calling this method from an onClick function). However when I call this method inside onActivityResult() as shown below, I can't hear a thing

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_running_ui);
    ...
    speakButton = (Button) findViewById(R.id.speakButton);
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if(activities.size()==0){
        speakButton.setEnabled(false);
        Toast.makeText(getApplicationContext(), "Speech Recognizer Not Found", 1000).show();
    }
    speakButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            startVoiceRecognitionActivity();
        }
    });

    commandText = (TextView) findViewById(R.id.command);
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
    switch(requestCode){

    case MY_DATA_CHECK_CODE:
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            // success, create the TTS instance
            mTts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {

                @Override
                public void onInit(int arg0) {
                    // TODO Auto-generated method stub
                    mTts.setLanguage(Locale.UK);

                }
            });


        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    case VOICE_RECOGNITION_REQUEST_CODE:
        if (resultCode == RESULT_OK){
            ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            commandText.setText(matches.get(0)); // get the first matching text

            speakText();

        }
    }
}

@Override
public void onPause(){
    if(mTts != null){
        mTts.stop();
        mTts.shutdown();
    }
    super.onPause();
}

What I'm trying to do is to let the android phone speak something after a speech to text conversion. I'm sure the program went in VOIC_RECOGNITION_REQUEST_CODE branch as it displayed the text with the commandText.setText(matches.get(0)); line.

I've got no clue why the textToSpeech works with a button press but not inside the onActivityResult method.

Was it helpful?

Solution

You shutdown the TextToSpeech in onPause, thus mTts is not binded to the Text to speech engine anymore. You need to move your code in onPause() to onStop() if what you do is to only to show the recognizer dialog.

When the speech recognizer dialog is shown, your activity onPause() is called, but not onStop() unless your activity is not visible anymore. You should also instanstiate the text to speech again in onStart().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top