由于某种原因,我无法从onutteranceCompleted调用包含PhoneCallListener的方法。没有错误消息,它似乎停止执行脚本。我添加了一些日志,它通过PhoneCallListener进入setUpPhone中的“here”日志。如果没有PhoneCallListener,它将进入最终日志“手机设置”。以下是我如何实现它的示例代码。
@Override
public void onInit(int status)
{
if(status == TextToSpeech.SUCCESS)
{
tts.setOnUtteranceCompletedListener(this);
int result = tts.setLanguage(Locale.US);
if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED)
{
//Language not supported
}
else
{
speakOut();
}
}
}
private void speakOut()
{
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, "Text to Speech");
tts.speak("blah blah", TextToSpeech.QUEUE_FLUSH, myHashAlarm);
}
@Override
public void onDestroy()
{
if(tts != null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onUtteranceCompleted(String utteranceId)
{
Log.i("TEST", utteranceId);
setUpPhone();
Log.i("TEST", "phone set up"); //this is never reached
}
private void setUpPhone()
{
Log.i("TEST", "here");
PhoneCallListener phoneListener = new PhoneCallListener(); //If I remove this line, the log "phone set up" displays.
}
答案 0 :(得分:0)
我不知道为什么PhoneCallListener会停止执行,但是我通过输入“phoneListener = new PhoneCallListener();”来解决这个问题。在onCreate方法中。