viewflipper と tts に問題があります。私がやりたいことは、viewflipper を使用して一連の単語を表示し、tts を 1 つずつ再生することです。まず、viewflipper に AnimationListener を設定し、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");
}
}
}