Convert Toast message to text

后端 未结 2 588
野趣味
野趣味 2021-01-22 07:59

How can I convert this TOAST message to voice in Android?

Example

Toast.makeText(MainActivity.this, \"I am enter code here\" +positive[+         


        
相关标签:
2条回答
  • 2021-01-22 08:22

    First import the package

    import android.speech.tts.TextToSpeech;
    

    Then initialize

    private TextToSpeech tts;
      tts = new TextToSpeech(this, this);
    

    Finally make a function like this

      private void speakOut() {
    
        String text = txtText.getText().toString();
    
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
    

    Woops. I forgot, you'll also need to define an onInit function

     public void onInit(int status) {
    
        if (status == TextToSpeech.SUCCESS) {
    
            int result = tts.setLanguage(Locale.US);
    
            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }
    
        } else {
            Log.e("TTS", "Initilization Failed!");
        }
    
    }
    

    In this example I used an onClickListener to call this function, using a button. Modify it to how you want to call this function when you toast a message.

    Just make the String text as your toast message. In the example above txtText was an editText. Modify as per your requirement

    0 讨论(0)
  • 2021-01-22 08:26
    TextView wordView = (TextView)view; String wordChosen = (String) wordView.getText(); Toast.makeText(MainActivity.this, "I am " + positive[+position] + " always", Toast.LENGTH_SHORT).show(); //tts.speak("I am" + blank +position+" always", TextToSpeech.QUEUE_FLUSH, null); tts.speak("You chose, you are "+wordChosen+" today and always", TextToSpeech.QUEUE_FLUSH, null); 
    
    0 讨论(0)
提交回复
热议问题