Mark
Mark

Reputation: 7718

TextToSpeech.OnInitListener.onInit(int) being called continuously

I'm getting reports that, on some (not all) HTC Desire HD (FRF91, 2.2) and HTC EVO 4G ( PC36100|3.29.651.5, 2.2), the TextToSpeech.OnInitListener.onInit(int) is being called repeatedly (over 1500 times in the space of a few seconds) on the same object. This behaviour does not occur for any of my other users (or with other Desire HD users) AFAICT.

The code is:

TextToSpeech tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
    private int mCallCount = 0; // trying to investigate potential infinite loops

    @Override
    public void onInit(int status) {
        if ((mCallCount % 100) == 1) {
            // report this
        }
        mCallCount++;
    }
});

Anyone any ideas?

EDIT: I have also tried calling the shutdown() method (the first time multiple listener calls are detected) but this doesn't seem to help.

Upvotes: 24

Views: 8318

Answers (3)

Mohit marwal
Mohit marwal

Reputation: 251

Try to create object of the Textospeech before on create ie. globally .try this code and check is it still calling many times????

public class TtsActivity extends Activity implements OnInitListener {

private int MY_DATA_CHECK_CODE = 0;

private TextToSpeech tts;

private EditText inputText;
private Button speakButton;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    inputText = (EditText) findViewById(R.id.input_text);
    speakButton = (Button) findViewById(R.id.speak_button);

    speakButton.setOnClickListener(new OnClickListener() {          
        @Override
        public void onClick(View v) {
            String text = inputText.getText().toString();
            if (text!=null && text.length()>0) {
                Toast.makeText(TtsActivity.this, "Saying: " + text, Toast.LENGTH_LONG).show();
                tts.speak(text, TextToSpeech.QUEUE_ADD, null);
            }
        }
    });

    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
            tts = 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) {
        Toast.makeText(TtsActivity.this, 
                "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
    }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(TtsActivity.this, 
                "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
    }
}

}

Upvotes: 0

camstuart
camstuart

Reputation: 623

This may or may not help, but I had a similar problem when call tts from a service, luckily for me I was better off doing my tts from an activity which solved the problem.

If you do this, and it is appropriate, make sure your manifest for the activity has:

android:finishOnTaskLaunch="true" 

Upvotes: 1

Ozzy
Ozzy

Reputation: 8322

Maybe you should get around it with your own intermediary method, for example:

private long lastCall = 0;
private long deepBreath = 5*1000; //5 seconds
private boolean hasRested;

TextToSpeech tts = new TextToSpeech(context, new TextToSpeech.OnInitListener() { 
    @Override 
    public void onInit(int status) { 
        long thisCall = Calendar.getInstance().getTimeInMillis();
        intermediaryMethod(status, thisCall);
    } 
}); 

//new method
public void intermediaryMethod(int status, long thisCall) {
    hasRested = (thisCall-lastCall)>=deepBreath;
    if (hasRested) {
        lastCall = thisCall;
        //do something about 'status'
    }
}

Upvotes: 1

Related Questions