Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid using deprecated API for UtteranceProgressListener

I am using android.speech.tts.TextToSpeech and would like to have an UtteranceProgressListener that does not override the deprecated onError but rather uses the recommended onError instead. Does anyone know how to do that? I want to avoid using a deprecated API in my app.

Building using Android Studio and without overriding the deprecated onError tells me that my Fragment "is not abstract and does not override abstract method onError(String) in UtteranceProgressListener". My current solution looks like this:

private final UtteranceProgressListener utteranceProgressListener = new UtteranceProgressListener() {
    /**
     * Called when an utterance "starts" as perceived by the caller. This will
     * be soon before audio is played back in the case of a {@link TextToSpeech#speak}
     * or before the first bytes of a file are written to the file system in the case
     * of {@link TextToSpeech#synthesizeToFile}.
     *
     * @param utteranceId The utterance ID of the utterance.
     */
    @Override
    public void onStart(String utteranceId) {

    }

    /**
     * Called when an utterance has successfully completed processing.
     * All audio will have been played back by this point for audible output, and all
     * output will have been written to disk for file synthesis requests.
     * <p>
     * This request is guaranteed to be called after {@link #onStart(String)}.
     *
     * @param utteranceId The utterance ID of the utterance.
     */
    @Override
    public void onDone(String utteranceId) {

    }

    /**
     * Called when an error has occurred during processing. This can be called
     * at any point in the synthesis process. Note that there might be calls
     * to {@link #onStart(String)} for specified utteranceId but there will never
     * be a call to both {@link #onDone(String)} and {@link #onError(String)} for
     * the same utterance.
     *
     * @param utteranceId The utterance ID of the utterance.
     * @deprecated Use {@link #onError(String, int)} instead
     */
    @Override
    public void onError(String utteranceId) {

    }

    @Override
    public void onError(String utteranceId, final int errorCode) {
       
    }
};

I looked here but was unable to see an improvement to my current solution.

like image 383
Spirit of the Void Avatar asked Sep 12 '25 09:09

Spirit of the Void


1 Answers

You can't- it's deprecated, but not removed. That means it needs to be defined. You can define it to do nothing, or to call onError(String, int) with a faked error code. But you can't not define it. It won't compile without it.

like image 79
Gabe Sechan Avatar answered Sep 14 '25 23:09

Gabe Sechan