Search code examples
androidgoogle-glassgoogle-gdk

Text to speech in google glass- only the last function call works


here is my code,

   @Override
          public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
              initialized = true;
              tts.setLanguage(Locale.ENGLISH);

              if (queuedText != null) {
                speak(queuedText);
              }
            }
          }

          public void speak(String text) {
            // If not yet initialized, queue up the text.
            if (!initialized) {
              queuedText = text;
              return;
            }
            queuedText = null;
            // Before speaking the current text, stop any ongoing speech.
            tts.stop();
            // Speak the text.
            tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
          }

I am calling the speak function like this,

speak("ten");
speak("nine");

I am able to hear only nine.I am trying to implement a counter, I will be adding delays between the calls.


Solution

  • There are, roughly, two related problems with how you're trying to speak things in sequence:

    1. tts.speak() is an async operation. It doesn't actually do the audio at that point, but queues it for the audio system to play. It usually plays shortly afterwards, but there is no guarantee that it will start speaking immediately.

    2. The call tts.stop() stops any speaking that was currently in progress, and using QUEUE_FLUSH means that any text that hadn't been playing yet won't be. So if you're calling speak("nine") immediately after speak("ten"), the "nine" immediately removes the "ten" from the queue.

    You have a couple of options, tho I don't know which one will meet your needs best.

    1. At the minimum, you should probably remove the tts.stop() part from the speech() function and use QUEUE_ADD instead.

    So changing the code to something like this, should work:

    public void speak(String text) {
      // If not yet initialized, queue up the text.
      if (!ttsInitialized) {
        queuedText = text;
        return;
      }
      queuedText = null;
      // Before speaking the current text, stop any ongoing speech.
      //tts.stop();
      // Speak the text.
      tts.speak(text, TextToSpeech.QUEUE_ADD, null);
    }
    
    1. If you need to wait some time period after the speaking completes, you should instead queue text with tts.speak() and monitor its progress with UtteranceProgressListener. When the UtteranceProgressListener.onDone() method is called, you should wait some period of time and then queue the next message to say.

    Code to do this might look something like

    private void countdown( final int start ){
      speak( ""+start );
      if( start > 0 ){
        Handler handler = new Handler();
        handler.postDelayed( new Runnable(){
          @Override
          public void run(){
            countdown( start-1 );
          }
        }, 1000 );
      }
    }
    

    and be called through

    countdown(10);