Text to speech(TTS)-Android

前端 未结 7 1018
星月不相逢
星月不相逢 2020-11-27 11:42

I am new to the android platform. Now I am working on TTS(Text to Speech).If I enter the text in a TextArea and I would like it to be converted to speech when i

7条回答
  •  感动是毒
    2020-11-27 12:02

    A minimalistic example to quickly test the TTS system:

    private TextToSpeech textToSpeechSystem;
    
    @Override
    protected void onStart() {
      super.onStart();
      textToSpeechSystem = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    String textToSay = "Hello world, this is a test message!";
                    textToSpeechSystem.speak(textToSay, TextToSpeech.QUEUE_ADD, null);
                }
            }
       });
    }
    

    If you don't use localized messages textToSpeechSystem.setLanguage(..) is important as well, since your users probably don't all have English set as their default language so the pronunciation of the words will be wrong. But for testing TTS in general this snippet is enough

    Related links: https://developer.android.com/reference/android/speech/tts/TextToSpeech

提交回复
热议问题