Android - how to know if TalkBack is speaking to interrupt TTS?

Viewed 921

I am working with TextToSpeech on Android to speak a sequence of texts. As it is a big string, I am using a new thread for this.

The problem is that when talkback speech accessibility is enabled, it stops reading my app to speak the notifications of the device, reads other buttons when the user touches the screen, etc.

I need to know when the talkback will speak, so I interrupt my thread and when the talkback finishes announcing whatever it is, my tts will be able to resume reading.

What is happening is that my tts reads line 1, line 2, line 3 (so talkback says something) and my tts did not read line 4, line 5 ... (...). It just resumes to speak after the talkback there to line 50, 60, since my repetition, that is in the thread, keeps running and the speaker is being occupied by the talkback.

Does anyone have any guidance / solution for this?

1 Answers

You can override onStop method with UtteranceProgressListener. When user who use talkback does anything you can be notified through this method. BTW, onStop is not an abstract method so you should override yourself. I hope you have found it already :)

textToSpeech.setOnUtteranceProgressListener(
        new UtteranceProgressListener() {

    @Override
    public void onStart(String utteranceId) {

    }

    @Override
    public void onDone(String utteranceId) {

    }

    @Override
    public void onStop(String utteranceId, boolean interrupted) {
        super.onStop(utteranceId, interrupted);
        yourCallback.stop();
    }

    @Override
    public void onError(String utteranceId) {

    }
});
Related