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