【问题标题】:text to speech android to output speech when Activity is visible文本到语音android在活动可见时输出语音
【发布时间】:2020-09-06 19:17:11
【问题描述】:

我正在为视障人士构建一个 android 原生应用,并且我想使用 android TTS - android.speech.tts.TextToSpeech 来指导用户使用我的应用。 单击按钮后,我成功地显示了演讲,但我还想在活动可见时输出欢迎消息。 这是一个代码sn-p:


public class MainActivity extends AppCompatActivity  {
    private TextToSpeech textToSpeech;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textToSpeech=new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status==TextToSpeech.SUCCESS){
                    int result=textToSpeech.setLanguage(Locale.getDefault());
                    if (result == TextToSpeech.LANG_MISSING_DATA
                            || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                        Log.e("TTS", "Language not supported");
                    } else {
                        Log.d("TTS","Speech initialized");
                    }
                } else {
                    Log.e("TTS", "Initialization failed");
                }
            }
        });
        speak("welcome");
        speak("Click on the button to begin settings ");

        audio.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                speak("this is a test");
            }
        });
    }
    public void speak(final String S){
    textToSpeech.speak(S,TextToSpeech.QUEUE_ADD,null);
    }
}

【问题讨论】:

    标签: java android text-to-speech


    【解决方案1】:

    问题是 tts 还没有初始化。

    修复它的最快方法是将介绍性的语音命令移到 onInit 中:

    @Override
                public void onInit(int status) {
                    if (status==TextToSpeech.SUCCESS){
                        int result=textToSpeech.setLanguage(Locale.getDefault());
                        if (result == TextToSpeech.LANG_MISSING_DATA
                                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                            Log.e("TTS", "Language not supported");
                        } else {
                            Log.d("TTS","Speech initialized");
    
                            speak("welcome");
                            speak("Click on the button to begin settings ");
                        }
                    } else {
                        Log.e("TTS", "Initialization failed");
                    }
                }
    

    【讨论】:

      【解决方案2】:

      基于 [https://stackoverflow.com/a/48354182/13518237][1] 我设法使它工作。 这是代码

      public class Speech  implements TextToSpeech.OnInitListener {
          private boolean initialized;
          private String queuedText;
          private String TAG = "TTS";
          private TextToSpeech tts; //text to speech
      
          public Speech() {
          }
      
          public TextToSpeech getTts() {
              return tts;
          }
      
          public void setTts(TextToSpeech tts) {
              this.tts = tts;
          }
          public void speak(String text) {
              if (!initialized) {
                  queuedText = text;
                  return;
              }
              queuedText = null;
              HashMap <String, String> params= new HashMap();
              params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"Done Speaking");
              tts.speak(text, TextToSpeech.QUEUE_ADD, params);
          }
          @Override
          public void onInit(int status) {
              if (status == TextToSpeech.SUCCESS) {
                  initialized = true;
                  tts.setLanguage(Locale.ENGLISH);
                  if (queuedText != null) {
                      speak(queuedText);
                  }
              }
          }
      }
      
      public class Menu extends AppCompatActivity {
          private RelativeLayout audio;
          private Speech speech = new Speech();
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_menu);
              audio= findViewById(R.id.audio);
              speech.setTts(new TextToSpeech(this, speech));
              speech.speak("This is the menu");
       audio.setOnClickListener(new View.OnClickListener(){
                  @Override
                  public void onClick(View v){
                      speak("this is a test");
                  }
              });
          }
      
          @Override
          public void onDestroy() {
              if (speech.getTts()!=null){
                  speech.getTts().stop();
                  speech.getTts().shutdown();
              }
              super.onDestroy();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2015-06-16
        • 1970-01-01
        • 2023-03-03
        • 2021-02-26
        • 2019-12-21
        • 2010-12-09
        • 2011-06-08
        • 1970-01-01
        • 2021-07-07
        相关资源
        最近更新 更多