5

我想知道 Google Glass 是否支持 TextToSpeech?

我做了这样的事情:

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

}

在我的活动(onCreate)中,我有:

controller_tts = new TextToSpeechController(getApplicationContext());

我面临几个问题:

  1. 首先onInit方法根本没有被调用,只有在我退出当前Activity的那一刻。
  2. 不知何故,使用 TTS 后,扬声器的音量变为静音,我无法从设置中调回音量(仅在我重新启动眼镜后)

难道我做错了什么?或者干脆谷歌眼镜不支持TTS,连想都不敢相信。

欢迎任何建议!非常感谢!:)

4

1 回答 1

4

您是否有可能在初始化 TextToSpeech 之前调用 stopTTS?

这在 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;
  }
}

在此示例中,无论何时您点击触摸板(或引发任何其他类型的运动事件),您都应该听到“Hello Glass”。请注意,如果在 TextToSpeech 初始化之前提供了文本,则将其排队,然后在初始化成功后说出。

这不包括任何拆卸,但要做到这一点,您始终可以onDestroy()在活动中停止/关闭 TextToSpeech。

于 2014-08-21T05:34:37.857 回答