说话失败:没有绑定到TTS引擎

时间:2014-02-02 10:40:26

标签: android text-to-speech voice-recognition

我正在尝试创建一个使用Speech来发送文本和Text to speech的应用程序 所以这个程序的算法是:
1 - 当用户运行该程序时,它将调用语音识别 2 - 在收到用户的输入后,程序将重复用户所说的相同单词。

这是我的代码:

 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;
}


   }

但应用程序不会说什么。在logcat中我得到一个错误,上面写着:
TextToSpeech(10335):说话失败:未绑定到TTS

在我提出这个申请之前,我是在不同的部分制作的:文本到语音,语音到文本 这两个应用程序都正常运行 我不知道为什么会出现这种错误 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

一个问题是你在打电话:

speakIt(FinalValue);

onActivityResult(

在onPause中,执行:

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

所以,一旦你打开你的子活动,onPause被调用并且tts被关闭,返回你使用这样的被破坏的tts这是错误的并且可能导致这样的行为。

您可以将代码从onPause移动到onStop,将初始代码移动到onStart。在onActivityResult中设置一些类实例变量,其数据在onInit内部说话。

相关问题