5

I was wondering if TextToSpeech is supported on Google Glass?

I did something like this:

public class TextToSpeechController implements TextToSpeech.OnInitListener{

private Context mContext;

private TextToSpeech tts;

public TextToSpeechController(Context context) {
    Log.e("TEXT TO SPEECH CONTROLLER", "controller");
    mContext = context;
    tts = new TextToSpeech(context, this);
}

@Override
public void onInit(int status) {
    Log.e("INIT TTS", "INIT");
    if (status == TextToSpeech.SUCCESS) {
            int result = tts.setLanguage(Locale.ENGLISH);

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                   Toast.makeText(mContext, "This Language is not supported", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(mContext, "Ready to Speak", Toast.LENGTH_LONG).show();
                speakTheText("Welcome to Vision Screening App");
            }

    } 
    else {
         Toast.makeText(mContext, "Can Not Speak", Toast.LENGTH_LONG).show();
    }   
}

public void stopTTS(){
    Log.e(".....TTS", "SHUTDOWN");
    tts.stop();
    tts.shutdown();
}

 public void speakTheText(String str){
     Log.e("SPEAK TEXT!!!!", "SPEAK TEXT");
     tts.speak(str, TextToSpeech.QUEUE_FLUSH, null);
 }

}

and in my Activity (onCreate) I have:

controller_tts = new TextToSpeechController(getApplicationContext());

I face several problems :

  1. First of all the onInit method is not called at all, only at the moment when I exit the current Activity.
  2. Somehow, after using TTS, the speeker's volume turns to mute and I cannot turn the volume back from the settings(only after I reboot the Glasses)

Am I doing something wrong? or simply Google Glass does not support TTS, even thought is hard to believe that.

Any suggestion is welcome! Thank you very much!:)

pt2121
  • 11,720
  • 8
  • 52
  • 69
Ispas Claudiu
  • 1,890
  • 2
  • 28
  • 54

1 Answers1

4

Is it possible that you are calling stopTTS before TextToSpeech is initialized?

This works just fine for me on Glass:

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.MotionEvent;
import android.widget.TextView;

import java.util.Locale;

public class TTSTestActivity extends Activity 
  implements TextToSpeech.OnInitListener {

  private TextToSpeech tts;
  private boolean initialized = false;
  private String queuedText;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView view = new TextView(this);
    view.setText("Tap Me");
    setContentView(view);
    tts = new TextToSpeech(this /* context */, this /* listener */);
  }

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

  @Override
  public boolean onGenericMotionEvent(MotionEvent event) {
    // On any motion event (including touchpad tap), say 'Hello Glass'
    speak("Hello Glass");
    return true;
  }
}

With this example, anytime you tap the touch pad (or cause any other type of motion event), you should hear "Hello Glass." Note that if text is provided before TextToSpeech has initialized, then this is queued and then spoken after initialization is a success.

This does not include any tear-down, but to do that you can always put stop/shutdown of TextToSpeech in onDestroy() of the activity.

Brandon Wuest
  • 206
  • 1
  • 2
  • Yes, your code works fine but I don't understand why my code doesn't. The difference that I implement TTS in a controller class and not in an Activity but it should be the same thing. Oh well the idea is that Google Glass supports TTS and it's just my code that it isn't all right. Thanks Brandon! – Ispas Claudiu Aug 21 '14 at 07:10
  • I found my problem. I tested TTS inside my project. Inside my activity I also have an AsynkTask which seems that it is blocking my TTS!! and I really don't know why – Ispas Claudiu Aug 21 '14 at 07:38
  • 3
    AsyncTask uses a shared single thread executor by default so its possible the TTS initialization is using an AsyncTask as well. For your AsyncTask, try calling `asyncTask.executeOnExecutor(Executors.newSingleThreadExecutor(), params)` to see if that unblocks the TTS initialization. – Brandon Wuest Aug 21 '14 at 14:54