简体   繁体   中英

android Text to Speech of a toast message

I am getting an error with the conversion of the text to speech in android studio. I have initialised the code but it still returns no voice outputs. The code is as follows.

    else if((match.contains("yes") || match.contains("yeah")) && defsele) {
            //Toast toast = Toast.makeText(getApplicationContext(), "Default selection is done and program is starting", Toast.LENGTH_SHORT);
            //toast.show();
            defsele=false;
            switch (progno) {
                case 1:
                    //Toast toast1 = Toast.makeText(getApplicationContext(),"The default settings for cotton cycle is done",Toast.LENGTH_SHORT);
                    //toast1.show();
                    String cotton = "The cotton program is starting with the default values";
                    tts.speak(cotton, TextToSpeech.QUEUE_FLUSH, null);
                    soak=true;
                    soakdef();
                    break;

The tts.speak gets depricated and does not function. How can i make this work.The initialisation code is as follows

    tts = new TextToSpeech(this,this);
  @Override
public void onInit(int status) {
    Log.d("Speech", "OnInit - Status ["+status+"]");
    if(status == TextToSpeech.SUCCESS){
        Log.d("Speech","Success");
        tts.setLanguage(Locale.ENGLISH);

I am very new to android programming and would appreciate any help.

Thanks in advanced!!!

try this

TextToSpeech textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int status) {
                            //your text
                            String textToSpeechStr = "Hello";

                            //status is success / 0
                            if (status == TextToSpeech.SUCCESS) {
                                //speech starts

                                textToSpeech.speak(textToSpeechStr, TextToSpeech.QUEUE_FLUSH, null);
                            }

                    }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM