Text to speech(TTS)-Android

前端 未结 7 859
星月不相逢
星月不相逢 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:11
    // variable declaration
    TextToSpeech tts;
    
    // TextToSpeech initialization, must go within the onCreate method
    tts = new TextToSpeech(getActivity(), new TextToSpeech.OnInitListener() {
     @Override
     public void onInit(int i) {
      if (i == TextToSpeech.SUCCESS) {
       int result = tts.setLanguage(Locale.US);
       if (result == TextToSpeech.LANG_MISSING_DATA ||
        result == TextToSpeech.LANG_NOT_SUPPORTED) {
        Log.e("TTS", "Lenguage not supported");
       }
      } else {
       Log.e("TTS", "Initialization failed");
      }
     }
    });
    
    // method call
    public void buttonSpeak().setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
      speak();
     }
    });
    }
    
    private void speak() {
     tts.speak("Text to Speech Test", TextToSpeech.QUEUE_ADD, null);
    }
    
    @Override
    public void onDestroy() {
     if (tts != null) {
      tts.stop();
      tts.shutdown();
     }
     super.onDestroy();
    }
    

    taken from: Text to Speech Youtube Tutorial

    0 讨论(0)
提交回复
热议问题