Java – why is the utteranceprogress listener not called to speak text?

I tried to call some methods to start and end text to speech, so I used setonutteranceprogresslistener, but it didn't work / get calls

What on earth did I do wrong?

Code required here:

Class:

public class SpeechRecognizerActivity extends Activity implements TextToSpeech.OnInitListener

Initialization method:

@Override
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        String language = Locale.getDefault().getLanguage();
        int result = tts.setLanguage(Locale.US);
        if (result == TextToSpeech.LANG_MISSING_DATA
                || result == TextToSpeech.LANG_NOT_SUPPORTED) {
            Log.e("TTS","This Language is not supported");
        } else {

        }
    } else {
        Log.e("TTS","Initilization Failed!");
    }
}

Way of speaking:

private void speakOut(String text) {
    setTtsListener();
    tts.setPitch(1.5f);
    tts.setSpeechRate(1.5f);
    tts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}

Listener (method called in speakout): none of these logs are displayed in logcat

private void setTtsListener() {
    if (Build.VERSION.SDK_INT >= 15)
    {
        int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
        {
            @Override
            public void onDone(String utteranceId)
            {
                Log.d(TAG,"progress on Done " + utteranceId);
            }

            @Override
            public void onError(String utteranceId)
            {
                Log.d(TAG,"progress on Error " + utteranceId);
            }

            @Override
            public void onStart(String utteranceId)
            {
                Log.d(TAG,"progress on Start " + utteranceId);
            }
        });
        if (listenerResult != TextToSpeech.SUCCESS)
        {
            Log.e(TAG,"Failed to add utterance progress listener");
        }
    }
    else
    {
        int listenerResult = tts.setOnUtteranceCompletedListener(new TextToSpeech.OnUtteranceCompletedListener()
        {
            @Override
            public void onUtteranceCompleted(String utteranceId)
            {
                Log.d(TAG,"progress on Completed " + utteranceId);
            }
        });
        if (listenerResult != TextToSpeech.SUCCESS)
        {
            Log.e(TAG,"Failed to add utterance completed listener");
        }
    }
}

I found a solution:

boolean speakingEnd = tts.isSpeaking();
    do {
        speakingEnd = tts.isSpeaking();
    } while ((speakingEnd));
    Log.d(TAG,"talking stopped");

I have added the end of a sayout method, but this solution is not very good Working with the audience will be perfect

What did I do wrong?

Solution

When you call TTS speak(text,null);

HashMap<String,String> map = new HashMap<String,String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
    tts.speak(text,map);

This lets texttospeech know to use your discourse listener for the text

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>