nr1
nr1

Reputation: 777

TTS (Text to Speech): manager in own class

i'm trying to get build a Text to Speech Manager within an own manager class.

Currently my code looks like this:

public class TTSManager extends Activity implements OnInitListener{

private TextToSpeech myTTS;
private int MY_DATA_CHECK_CODE = 0;
private boolean readyToSpeak = false;

public TTSManager()
{
    ;
}

public void checkIfTTSModulesInstalled()
{
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
}


protected void onActivityResult(
        int requestCode, int resultCode, Intent data) {
    if (requestCode == MY_DATA_CHECK_CODE) {
        if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) 
        {
            // success, create the TTS instance
            myTTS = new TextToSpeech(this, this);
        } 
        else 
        {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }
}


@Override
public void onInit(int status) 
{
    if (status == TextToSpeech.SUCCESS)
    {
        if(myTTS.isLanguageAvailable(Locale.US) == TextToSpeech.LANG_AVAILABLE)
        {
            myTTS.setLanguage(Locale.US);
            readyToSpeak = true;
        }
    }
}

public void speak(String text)
{
    if (readyToSpeak)
        myTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

}

Unfortunately i'm receiving a null pointer exception when trying to initialize the TTS module. How can i build a TTS Manager is an own class (and not directly within the activity where the TTS should be used)?

Upvotes: 0

Views: 2920

Answers (2)

nr1
nr1

Reputation: 777

I now did it like this (although not being sure if this way is "ok"):

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;

public class TTSManager
{
private TextToSpeech myTTS;
private boolean readyToSpeak = false;
private Context context;

public TTSManager(Context baseContext)
{
    this.context = baseContext;
}

public void initOrInstallTTS()
{
    myTTS = new TextToSpeech(context, new OnInitListener() 
    {               
        @Override
        public void onInit(int status) 
        {
            if (status == TextToSpeech.SUCCESS)
            {
                myTTS.setLanguage(Locale.US);
                readyToSpeak = true;
            }
            else
                installTTS();
        }
    });
}

private void installTTS()
{
    Intent installIntent = new Intent();
    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
    context.startActivity(installIntent);
}

public void speak(String text)
{       
    if (readyToSpeak)
        myTTS.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

}

Upvotes: 2

Nikolay Elenkov
Nikolay Elenkov

Reputation: 52936

Don't have your manager class extend Activity. If you do, there won't be a way to use it in other activities, which is, I think, your goal (it has to be initialized by Android, simply calling new TTSManager() doesn't make it a valid activity). Make it a regular class, and pass a Context parameter to all methods that need it.

As for the NPE, you might want to look at the stack trace.

Here's something similar I wrote, might give you a few pointers. It has a lot of code for switching engines, but you can ignore that if you are just using the default one:

http://code.google.com/p/wwwjdic/source/browse/branches/2.0/wwwjdic/src/org/nick/wwwjdic/TtsManager.java

Upvotes: 2

Related Questions