为什么没有UtteranceProgress Listener在Text to Speech上被调用?

use*_*332 16 java android text-to-speech complete listener

我试图在文本的开头和结尾调用一些方法,所以我使用了setOnUtteranceProgressListener,但它不能工作/被调用.

我究竟做错了什么?

这里需要的代码:

类:

public class SpeechRecognizerActivity extends Activity implements TextToSpeech.OnInitListener
Run Code Online (Sandbox Code Playgroud)

初始化方法:

 @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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

说话开始的方法:

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

监听器(方法在speakOut中调用):这些日志都不会显示在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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到的一个小解决方法是:

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

我已将它添加到speakOut方法的结尾,但这个解决方案并不是很好.与听众合作将是完美的.

那么我做错了什么?

小智 31

当你打电话时tts.speak(text, TextToSpeech.QUEUE_FLUSH, null); 你需要传递一个带有KEY_PARAM_UTTERANCE_ID的地图

    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"messageID");
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
Run Code Online (Sandbox Code Playgroud)

这让TextToSpeech知道如何使用你的话语听众 text