Search code examples
androidtext-to-speech

how to use text to speech with multiple values in android one by one


I am working on TTS(text-to-speech) and I have downloaded a demo from the internet. I have successfully run it. I would like to now modify this ,I am having multiple values on the screen and want each word to be spoken individually.

Here is my code:

package com.androidhive.texttospeech;

import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
    /**
     * Called when the activity is first created.
     */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;

    String one, two, three, four;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        one = "Hello";
        two = "how";
        three = "are";
        four = "you";

        tts = new TextToSpeech(this, this);

        btnSpeak = (Button) findViewById(R.id.btnSpeak);

        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.US);

            // tts.setPitch(5); // set pitch level

            // tts.setSpeechRate(2); // set speech speed rate

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed");
        }

    }

    private void speakOut() {

        private void speakOut () {

            String que = answer_question.getText().toString();
            String op1 = opt_1.getText().toString();
            String op2 = opt_2.getText().toString();
            String op3 = opt_3.getText().toString();
            String op4 = opt_4.getText().toString();
            tts.speak(que, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op1, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op2, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op3, TextToSpeech.QUEUE_FLUSH, null);
            tts.speak(op4, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

Solution

  • Try this.

            private void speakOut () {
    
                String que = answer_question.getText().toString();
                String op1 = opt_1.getText().toString();
                String op2 = opt_2.getText().toString();
                String op3 = opt_3.getText().toString();
                String op4 = opt_4.getText().toString();
                tts.speak(que, TextToSpeech.QUEUE_ADD, null);
                tts.speak(op1, TextToSpeech.QUEUE_ADD, null);
                tts.speak(op2, TextToSpeech.QUEUE_ADD, null);
                tts.speak(op3, TextToSpeech.QUEUE_ADD, null);
                tts.speak(op4, TextToSpeech.QUEUE_ADD, null);
            }
    

    It will speak each words sequentially.

    Or, if you want speak next word after previous word is spoken, try to use UtteranceProgressListener like this.

    package com.androidhive.texttospeech;
    
    import java.util.Locale;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.speech.tts.TextToSpeech;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class AndroidTextToSpeechActivity extends Activity implements TextToSpeech.OnInitListener {
    /**
     * Called when the activity is first created.
     */
    
    private HashMap<String, String> utterParam;
    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    
    String one, two, three, four;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        one = "Hello";
        two = "how";
        three = "are";
        four = "you";
    
        tts = new TextToSpeech(this, this);
    
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
    
        txtText = (EditText) findViewById(R.id.txtText);
    
        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                speakOut();
            }
    
        });
    }
    
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }
    
    @Override
    public void onInit(int status) {
        // TODO Auto-generated method stub
        utterParam = new HashMap<String, String>();
        utterParam.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "utterID");
    
        if (status == TextToSpeech.SUCCESS) {
    
            int result = tts.setLanguage(Locale.US);
    
            // tts.setPitch(5); // set pitch level
    
            // tts.setSpeechRate(2); // set speech speed rate
    
            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();
            }
    
        } else {
            Log.e("TTS", "Initilization Failed");
        }
    
    }
    
    private void speakOut() {
    
        private void speakOut () {
    
            String que = answer_question.getText().toString();
            //String op1 = opt_1.getText().toString();
            //String op2 = opt_2.getText().toString();
            //String op3 = opt_3.getText().toString();
            //String op4 = opt_4.getText().toString();
            tts.speak(que, TextToSpeech.QUEUE_FLUSH, utterParam);
            //tts.speak(op1, TextToSpeech.QUEUE_FLUSH, null);
            //tts.speak(op2, TextToSpeech.QUEUE_FLUSH, null);
            //tts.speak(op3, TextToSpeech.QUEUE_FLUSH, null);
            //tts.speak(op4, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
    
    private UtterProgressListener utterListener = new UtterProgressListener() {
        @Override
        public void onDone(String utteranceId) {
            // TODO Auto-generated method stub
            if (utteranceId.equals("utterID")) {
                // This code will be activate when TextToSpeech is stopped or done.(under API 22)
                // This code will be activate when TextToSpeech is done.(over API 23)
            }
        }
    
        @Override
        public void onError(String utteranceId) {
            // TODO Auto-generated method stub
            if (utteranceId.equals("utterID")) {
    
            }
        }
    
        @Override
        public void onStart(String utteranceId) {
            // TODO Auto-generated method stub
            if (utteranceId.equals("utterID")) {
    
            }
        }
    
        @TargetApi(23)
        @Override
        public void onStop(String utteranceId, boolean interrupted) {
            if (utteranceId.equals("utterID")) {
                  // This code will be activate when TextToSpeech is stopped.(overr API 23)
            }
        }
    };