【问题标题】:Is TextToSpeech supported on Google Glass?Google Glass 是否支持 TextToSpeech?
【发布时间】:2014-10-13 22:59:29
【问题描述】:

我想知道 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);
 }

}

在我的 Activity (onCreate) 中,我有:

controller_tts = new TextToSpeechController(getApplicationContext());

我面临几个问题:

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

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

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

【问题讨论】:

    标签: text-to-speech google-glass google-gdk google-text-to-speech


    【解决方案1】:

    是否有可能在 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 的停止/关闭。

    【讨论】:

    • 是的,您的代码工作正常,但我不明白为什么我的代码不能。我在控制器类中实现 TTS 而不是在 Activity 中实现 TTS 的区别,但它应该是同一件事。哦,这个想法是谷歌眼镜支持 TTS,只是我的代码不太好。谢谢布兰登!
    • 我发现了我的问题。我在我的项目中测试了 TTS。在我的活动中,我还有一个 AsynkTask 似乎它正在阻止我的 TTS!我真的不知道为什么
    • AsyncTask 默认使用共享单线程执行器,因此 TTS 初始化也可能使用 AsyncTask。对于您的 AsyncTask,请尝试调用 asyncTask.executeOnExecutor(Executors.newSingleThreadExecutor(), params) 以查看这是否会解除对 TTS 初始化的阻止。
    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2016-06-09
    • 2015-10-27
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多