Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

android - Why is UtteranceProgressListener not an interface?

I'm playing around with Android's TTS features and the TextToSpeech class has this method to set a listener which gets notified once the TextToSpeech has finished playing:

public int setOnUtteranceCompletedListener(TextToSpeech.OnUtteranceCompletedListener listener)

But the OnUtteranceCompletedListener is defined as public abstract class. As my MainActivity already extends Activity, it can't extend OnUtteranceCompletedListener as well. I could use the older method with a OnUtteranceCompletedListener, but this is deprecated:

public int setOnUtteranceCompletedListener (TextToSpeech.OnUtteranceCompletedListener listener)`

Why is OnUtteranceCompletedListener not defined as public static interface? I'm thinking to write my own UtteranceProgressListenerImpl, which will then just call the MainActivitys onDone method. Is this the proper way or is there a better/cleaner alternative?

private class UtteranceProgressListenerImpl extends UtteranceProgressListener {

    private MainActivity mainActivity;

    UtteranceProgressListenerImpl(MainActivity mA) {
        mainActivity = mA;
    }

    @Override
    public void onDone(String utteranceId) {
        mainActivity.onDone(utteranceId);
    }

    @Override
    public void onError(String utteranceId) { /* empty */ }

    @Override
    public void onStart(String utteranceId) { /* empty */ }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't know I think it should be an interface as well. I use this code to get around it. It is available here as well.

Also, vote for this bug I submitted a while ago.

public void setTts(TextToSpeech tts)
    {
        this.tts = tts;
        if (Build.VERSION.SDK_INT >= 15)
        {
            tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
            {
                @Override
                public void onDone(String utteranceId)
                {
                    onDoneSpeaking(utteranceId);
                }

                @Override
                public void onError(String utteranceId)
                {
                }

                @Override
                public void onStart(String utteranceId)
                {
                }
            });
        }
        else
        {
            Log.d(TAG, "set utternace completed listener");
            tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener()
            {
                @Override
                public void onUtteranceCompleted(String utteranceId)
                {
                    onDoneSpeaking(utteranceId);
                }
            });
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...