Android viewflipper:如何自动翻转,播放文字转语音和翻转

时间:2013-02-14 04:06:18

标签: android text-to-speech viewflipper

我在viewflipper和tts上遇到了一些麻烦。我想要做的是使用viewflipper逐个显示一系列单词和播放tts。首先,我为viewflipper设置AnimiationListener并在onAnimationEnd()中播放TTS。其次,我为TTS设置setOnUtteranceProgressListener()并尝试在onDone()事件中调用viewflipper.showNext()。但我得到以下错误。似乎viewflipper实例在TTS中不可访问。任何人都可以给我提示解决这个或任何其他方式可以做同样的事情吗?

错误消息

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

0 个答案:

没有答案