我在 viewflipper 和 tts 上有一些麻烦。我想要做的是使用viewflipper显示一系列单词并逐个播放tts。首先,我为 viewflipper 设置了 AnimiationListener 并在 onAnimationEnd() 中播放 TTS。其次,我为 TTS 设置了 setOnUtteranceProgressListener() 并尝试在 onDone() 事件中调用 viewflipper.showNext()。但我收到以下错误。在 TTS 中似乎无法访问 viewflipper 实例。任何人都可以给我提示来解决这个问题或任何其他方式可以做同样的事情?
错误信息
02-14 12:00:44.934: E/JavaBinder(3321): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
02-14 12:00:44.934: E/JavaBinder(3321): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2588)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:608)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.View.setFlags(View.java:8429)
02-14 12:00:44.934: E/JavaBinder(3321): at android.view.View.setVisibility(View.java:5714)
02-14 12:00:44.934: E/JavaBinder(3321): at android.widget.ViewAnimator.showOnly(ViewAnimator.java:159)
02-14 12:00:44.934: E/JavaBinder(3321): at android.widget.ViewAnimator.showOnly(ViewAnimator.java:179)
02-14 12:00:44.934: E/JavaBinder(3321): at android.widget.ViewAnimator.setDisplayedChild(ViewAnimator.java:111)
02-14 12:00:44.934: E/JavaBinder(3321): at android.widget.ViewAnimator.showNext(ViewAnimator.java:130)
02-14 12:00:44.934: E/JavaBinder(3321): at com.example.reading.TTS_Play$3.onDone(TTS_Play.java:305)
02-14 12:00:44.934: E/JavaBinder(3321): at android.speech.tts.TextToSpeech$Connection$1.onDone(TextToSpeech.java:1264)
02-14 12:00:44.934: E/JavaBinder(3321): at android.speech.tts.ITextToSpeechCallback$Stub.onTransact(ITextToSpeechCallback.java:63)
02-14 12:00:44.934: E/JavaBinder(3321): at android.os.Binder.execTransact(Binder.java:351)
02-14 12:00:44.934: E/JavaBinder(3321): at dalvik.system.NativeStart.run(Native Method)
viewflipper 动画结束时触发 TTS
@Override
public void onAnimationEnd(Animation animation) {
flipper.stopFlipping();
String utteranceID = "some messages";
playTTS(utteranceID); // play tts
}
初始化 TTS 并在 viewflipper 上显示第一个视图
@Override
public void onInit(int status) {
setTtsListener(tts); // set TTS listener
flipper.stopFlipping(); // stop viewflipper to auto flip
String utteranceID = "some messages";
playTTS(utteranceID); // play tts
}
播放 TTS
private void playTTS(String utteranceID) {
View v = flipper.getCurrentView();
TextView tv_tts = (TextView) v.findViewById(R.id.tv_tts);
HashMap<String, String> ttsHashMap = new HashMap<String, String>();
ttsHashMap.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, utteranceID);
tts.speak(tv_tts.getText().toString(), TextToSpeech.QUEUE_FLUSH, ttsHashMap);
}
TTS 监听器
@SuppressLint("NewApi") @SuppressWarnings("deprecation")
private void setTtsListener(TextToSpeech tts) {
this.tts = tts;
if (Build.VERSION.SDK_INT >= 15) {
int listenerResult = this.tts.setOnUtteranceProgressListener(
new UtteranceProgressListener() {
@Override
public void onDone(String utteranceId) {
// this.onDone(utteranceId);
flipper.showNext(); // <-- throws errors here
}
@Override
public void onError(String utteranceId) {
}
@Override
public void onStart(String utteranceId) {
}
});
if (listenerResult != TextToSpeech.SUCCESS) {
Log.d(LOG_TAG, "failed to add utterance progress listener");
}
} else {
int listenerResult = tts.setOnUtteranceCompletedListener(
new OnUtteranceCompletedListener() {
@Override
public void onUtteranceCompleted(String utteranceId) {
// this.onDone(utteranceId);
flipper.showNext();
}
});
if (listenerResult != TextToSpeech.SUCCESS) {
Log.d(LOG_TAG, "failed to add utterance completed listener");
}
}
}