ammukuttylive
ammukuttylive

Reputation: 365

Text to speech-not speaking out

I am confused with a code which will speaks out the word from an edit text,The code i used is not speaking out but it getting the value,no of characters etc correctly.

The code i used is..

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

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    System.out.println("Entered to the Activity");
    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) {
        System.out.println("Entered  to OnDestroy");
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

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

    System.out.println("Enterd init Function");
    if (status == TextToSpeech.SUCCESS) {

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

        // 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() {
     System.out.println("Entered Speakout");
    String text = txtText.getText().toString();

    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

It returning a message that languagge not supported..How can i rectify it

Upvotes: 1

Views: 4917

Answers (2)

GrIsHu
GrIsHu

Reputation: 23638

Try out below code. It works fine for me i hope it will help you also.

public class TexttoSpeechActivity extends Activity implements OnClickListener,
    OnInitListener {
    private Button m_btnSpech;
    private EditText m_etText;
    // TTS object
    private TextToSpeech m_tts;
    // status check code
    private final int m_data = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        m_btnSpech = (Button) findViewById(R.id.mbtnSpeak);
        m_etText = (EditText) findViewById(R.id.metTextValues);
        // listen for clicks
        m_btnSpech.setOnClickListener(this);        
        // check for TTS data intent to check if a TTS engine is installed
        Intent m_intent = new Intent();
        m_intent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(m_intent, m_data);
    }
    // respond to button click.
    @Override
    public void onClick(View p_v) {
        // get the text entered.
        String m_word = m_etText.getText().toString();
        speakWord(m_word);
    }
    /**
     * Executed when a new TTS is instantiated. Some text is spoken via TTS
     * here.
     * 
     * @param p_word
     *            -contains the word entered into the edittext.
     */
    private void speakWord(String p_word) {
        // speak straight away
        m_tts.speak(p_word, TextToSpeech.QUEUE_FLUSH, null);
    }
    /**
     * This is the callback from the TTS engine check, if a TTS is installed we
     * create a new TTS instance (which in turn calls onInit), if not then we
     * will create an intent to go off and install a TTS engine
     * 
     * @param p_requestCode
     *            int Request code returned from the check for TTS engine.
     * @param p_resultCode
     *            int Result code returned from the check for TTS engine.
     * @param p_data
     *            Intent Intent returned from the TTS check.
     */
    @Override
    protected void onActivityResult(int p_requestcode, int p_resultcode,
            Intent p_data) {
        if (p_requestcode == m_data) {
            if (p_resultcode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // the user has the necessary data - create the TTS
                m_tts = new TextToSpeech(this, this);
            } else {
                // no data - install it now
                Intent m_intnt = new Intent();
                m_intnt.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(m_intnt);
            }
        }
    }
    // setup TTS
    @Override
    public void onInit(int p_status) {
        // check for successful instantiation
        if (p_status == TextToSpeech.SUCCESS) {
            if (m_tts.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE) {
                m_tts.setLanguage(Locale.US);
                Toast.makeText(this, "Text To Speech ", Toast.LENGTH_LONG)
                        .show();
            }
            Toast.makeText(TexttoSpeechActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        } else if (p_status == TextToSpeech.ERROR) {
            Toast.makeText(this, "Sorry! Text To Speech failed...",
                    Toast.LENGTH_LONG).show();
        }
    }
    /**
     * Be kind, once you've finished with the TTS engine, shut it down so other
     * applications can use it without us interfering with it.
     */
    @Override
    public void onDestroy() {
        // Don't forget to shutdown!
        if (m_tts != null) {
            m_tts.stop();
            m_tts.shutdown();
        }
        super.onDestroy();
    }
}

Upvotes: 1

Ajay S
Ajay S

Reputation: 48622

Try this it might help you.

I think in your device speech synthesizer is not installed so use these for this. If not installed it will redirect to google play to install this.

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

Upvotes: 6

Related Questions