简体   繁体   中英

Speak failed: not bound to TTS Engine

I am trying to make an application that uses Speech to text, and Text to speech.
So the algorithm of this program is:
1 - when the user runs this program, it will call voice Recognition 2 - after getting the input from the user, the program will repeat the same word the user said.

here's my code:

 public class MainActivity extends Activity implements TextToSpeech.OnInitListener, OnClickListener {

protected static final int REQUEST_OK = 1234;
String userSay;
TextView text1;
private TextToSpeech tts;

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

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    findViewById(R.id.button1).setOnClickListener(this);
    text1 = (TextView) findViewById(R.id.text1);
    tts=new TextToSpeech(this, this);
}

@Override
public void onDestroy() {
    // Don't forget to shutdown tts!
    if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

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

@Override
public void onInit(int status) {

    if (status == TextToSpeech.SUCCESS) {
        Log.e("TTS", "Initilization Success!");        
        int result = tts.setLanguage(Locale.US);

        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS", "This Language is not supported");
        } else {
            String anyThing = " ";
            speakIt(anyThing);
        }
        return;
    } else {
        Log.e("TTS", "Initilization Failed!");
    }

}

private void speakIt(String someThing) {
    Log.e("something: ", someThing);
        tts.speak(someThing, TextToSpeech.QUEUE_ADD, null);
        Log.e("TTS", "called");
}

@Override
public void onClick(View v) {
    //Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
     //i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
    text1.setText(" ");
    Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    i.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
    i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    i.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    i.putExtra(RecognizerIntent.EXTRA_PROMPT, "I'm listen to you...");
     try {
         startActivityForResult(i, REQUEST_OK);
     } catch (Exception e) {
         Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
     }

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==REQUEST_OK  && resultCode==-1) {
        ArrayList<String> thingsYouSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

        userSay = thingsYouSaid.get(0);

    }
    try {
        String FinalValue = getDataMethod(hasil);
        text1.setText(FinalValue);
        Log.v("Status OK: ", FinalValue);
        speakIt(FinalValue);
        Log.v("speakIt: ", "called");
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    super.onActivityResult(requestCode, resultCode, data);
}

private String getDataMethod(String num) throws IllegalStateException, IOException {
    num = "You say, " + num;
    return num;
}


   }

But the application won't say anything. And in the logcat I got an error that says:
TextToSpeech(10335): speak failed: not bound to TTS

When before I made this application, I made it in separate parts: Text to Speech, and Speech to Text.
Both of that applications ran normally.
I don't know why this error occurs.
Can anyone help me?

One problem is that you are calling:

speakIt(FinalValue);

inside:

onActivityResult(

while in onPause, you execute:

if(tts !=null){
    tts.stop();
    tts.shutdown();
}

so once, you open your sub activity, onPause is called and tts is shutdown, returning back you use such destroyed tts which is wrong and can cause such behaviour.

You can move your code from onPause to onStop, and your init code to onStart. In onActivityResult set some class instance variable with data to speak inside onInit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM