0

我试图在完成讲话后完成活动,但由于某种原因,我无法理解它告诉我 setOnUtteranceCompleted 不适用于文本到语音。我是android编程新手,所以请温柔:-)

这是代码...

public class SpeakActivity extends Activity implements OnUtteranceCompletedListener{

    Random randnum = new Random();
    TextToSpeech tts = null;
    private boolean ttsIsInit = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speak);
        // Show the Up button in the action bar.
        setupActionBar();
        startTextToSpeech();
    }

    void startTextToSpeech(){
        final int randint = randnum.nextInt(4);
        final String text = ((GlobVars) this.getApplication()).getResponse(randint);
        tts = new TextToSpeech(this, new OnInitListener() {
            public void onInit(int status) {
                tts.setOnUtteranceCompletedListener(this);
                if (status == TextToSpeech.SUCCESS) {
                    ttsIsInit = true;
                    if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0){
                        tts.setLanguage(Locale.ENGLISH);
                    }
                    tts.setPitch(0.5f);
                    tts.setSpeechRate(0.5f);
                    if (tts != null && ttsIsInit) {
                        Log.d("got ere", "spoken");
                        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
                    }
                }
            }
        });
    }


    // shut down tts to free the TTS resources
   @Override
   public void onDestroy() {
      if (tts != null) {
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
 }


    @Override
    public void onUtteranceCompleted(String arg0) {
        ((GlobVars) this.getApplication()).setListen(true);
        this.finish();

    }


}
4

1 回答 1

0

我不确定,但根据setOnUtteranceCompletedListener()的文档,您可能需要使用TextToSpeech.OnUtteranceCompletedListener 侦听器作为参数。我认为使用该功能的方法如下。runOnUIThread请注意,如果您想在调用 onUtteranceCompleted 函数时对 UI 进行任何更改,请注意使用方法。

TextToSpeech tts= new TextToSpeech(context, new OnInitListener() {
    @Override
    public void onInit(int status) {
        tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() {
            @Override
            public void onUtteranceCompleted(String utteranceId) {
                //Do things here
            }
        });
     }
});

以上来源:检查onUtteranceCompleted 没有被调用?问题。

希望这可以帮助。

于 2013-07-12T20:45:52.460 回答