Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global TTS in Android

Hi I am devloping an application for blind users so that I use very often text to speech as practicaly the only one method how to respond on user actions. I decided to make one global TTS instance running as long as the app. I have implemented it this way

package com.simekadam.blindguardian;

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

public class SpeechHelper implements OnInitListener {

private static TextToSpeech mTts;
private String text; 
private static final SpeechHelper helper = new SpeechHelper();

public static SpeechHelper getInstance(){

    return helper;
}


public void say(String text, Context context){

    if(mTts == null){
        this.text = text;
        mTts = new TextToSpeech(context, (OnInitListener) helper);

    }
    else{
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}


@Override
public void onInit(int status) {
    // TODO Auto-generated method stub
    if (status == TextToSpeech.SUCCESS) {
        mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }
}

public void stopTTS(){
    if(mTts != null){
        mTts.shutdown();
        mTts.stop();
        mTts = null;
    }
}

}

At first - its working BUT ..I wanted to check the availability of speech data like that

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Intent checkIntent = new Intent();
    checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    text = getIntent().getExtras();
}


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
            mTts = new TextToSpeech(this, (OnInitListener) this);
            mTts.setLanguage(new Locale("cze", "CZE"));
        } else {
            // missing data, install it
            Intent installIntent = new Intent();
            installIntent.setAction(
                TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
            startActivity(installIntent);
        }
    }       
}

Its code from Android developer portal, but I cant start Activity for result from class which is not child of android.Activity.. Please how to check it without using activities, and is it this approach of invoke TTS correct? (I have implemented it all with Activities before, but there was a couple of memory leaks, due to incorrectly closed TTS - and when I closed it properly, it must been created again on every call - just too slow..)

like image 283
simekadam Avatar asked Feb 15 '26 21:02

simekadam


2 Answers

Initialize your global instance from onActivityResult(), after you know that TTS data is available. Your app needs an activity, so do it from the entry activity, all subsequent ones will be able to use your global instance once it is initialized. Also think about when and how you will shut it down.

like image 126
Nikolay Elenkov Avatar answered Feb 17 '26 12:02

Nikolay Elenkov


You don't need to use the ACTION_CHECK_TTS_DATA. Instead use isLanguageAvailable like this: (make sure to call this only after onInit is complete)

    // check if language is available
    switch (tts.isLanguageAvailable(locale))
    {
        case TextToSpeech.LANG_AVAILABLE:
        case TextToSpeech.LANG_COUNTRY_AVAILABLE:
        case TextToSpeech.LANG_COUNTRY_VAR_AVAILABLE:
            Log.d(TAG, "SUPPORTED");
            break;
        case TextToSpeech.LANG_MISSING_DATA:
            Log.d(TAG, "MISSING_DATA");//launch the install data activity
            break;
        case TextToSpeech.LANG_NOT_SUPPORTED:
            Log.d(TAG, "NOT SUPPORTED");//report failure
            break;
    }
like image 33
gregm Avatar answered Feb 17 '26 12:02

gregm