避免对 UtteranceProgressListener 使用已弃用的 API

Avoid using deprecated API for UtteranceProgressListener

我正在使用 android.speech.tts.TextToSpeech 并希望有一个不覆盖 deprecated onError but rather uses the recommended onError 的 UtteranceProgressListener。有谁知道这是怎么做到的吗?我想避免在我的应用中使用已弃用的 API。

使用 Android Studio 构建并且没有覆盖已弃用的 onError 告诉我我的片段“不是抽象的并且不会覆盖 UtteranceProgressListener 中的抽象方法 onError(String)”。我当前的解决方案如下所示:

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) {
       
    }
};

我查看了 here,但看不到我当前解决方案的改进。

你不能 - 它已被弃用,但并未删除。这意味着它需要被定义。您可以将其定义为不执行任何操作,或者使用伪造的错误代码调用 onError(String, int)。但你不能不定义它。没有它就无法编译。