Making a Text-To-Speech Wrapper in Android

Posted by John Montgomery on Stack Overflow See other posts from Stack Overflow or by John Montgomery
Published on 2010-04-06T18:09:39Z Indexed on 2010/04/06 18:13 UTC
Read the original article Hit count: 582

Filed under:
|
|
|

I am attempting to create a wrapper class for Google Android's Text-To-Speech functionality. However, I'm having trouble finding a way to have the system pause until after the onInit function has finished. Attached at the bottom is something of a solution I created based on what I found here: http://stackoverflow.com/questions/1160876/android-speech-how-can-you-read-text-in-android

However, this solution does not seem to work. Any thoughts on why this might not be working, or what would be a good idea in order to make sure that any Speak() calls happen after my onInit() call?

public class SpeechSynth implements OnInitListener {

private TextToSpeech tts;
static final int TTS_CHECK_CODE = 0;
private int ready = 0;
private ReentrantLock waitForInitLock = new ReentrantLock();

SpeechSynth( Activity screen )
{
    ready = 0;
    tts = new TextToSpeech( screen, this );
    waitForInitLock.lock();

}

public void onInit(int status) 
{
    if (status == TextToSpeech.SUCCESS)
    {
        ready = 1;
    }
    waitForInitLock.unlock();
}

public int Speak( String text )
{
    if( ready == 1 )
    {
        tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        return 1;
    }
    else
    {
        return 0;
    }   
}

}

I have been able to make it so that I can pass a string of text through the constructor, then have it played in the onInit() function. However, I would really like to avoid having to destroy and re-create the whole text-to-speech engine every time I need to have my program say something different.

© Stack Overflow or respective owner

Related posts about android

Related posts about text-to-speech