java – 为什么UtteranceProgress侦听器没有被调用文本来说话?

前端之家收集整理的这篇文章主要介绍了java – 为什么UtteranceProgress侦听器没有被调用文本来说话?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图调用一些方法开始和结束文本到语音,所以我使用setOnUtteranceProgressListener,但它不工作/获取调用.

我究竟做错了什么?

这里需要的代码

类:

public class SpeechRecognizerActivity extends Activity implements TextToSpeech.OnInitListener

初始化方法

@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!");
    }
}

发言方式:

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

监听器(方法调用在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");
        }
    }
}

我发现一点解决方法是:

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

我已经添加了一个sayOut方法的结尾,但这个解决方案不是很好.
与听众合作将是完美的.

那我做错了什么?

解决方法

当你调用tts.speak(text,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,map);

这可以让TextToSpeech知道为该文本使用您的话语侦听器

猜你在找的Java相关文章