android - 即使在传递 HashMap 参数后也不会调用话语进度监听器

标签 android speech-recognition text-to-speech

这是我的代码,我有一系列问题会被 TTS 询问,每个问题后语音识别器都会被调用。我的话语监听器永远不会被调用。

   @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_speech_recognizer);
            tts = new TextToSpeech(this /* context */, this /* listener */);
}

//This is called after first time user clicks a button

    private void processEnquiry() {
            // TODO Auto-generated method stub
            for(int i=0;i<EnquiryList.size();i++)
            {
                speak(EnquiryList.get(i).toString());

            }
        }

        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                initialized = true;
                tts.setLanguage(Locale.ENGLISH);
                if (queuedText != null) {
                    speak(queuedText);
                }
            }
        }

        public void speak(String text) {
            // If not yet initialized, queue up the text.
            if (!initialized) {
                queuedText = text;
                return;
            }
            queuedText = null;
            // Before speaking the current text, stop any ongoing speech.
            //tts.stop();
            // Speak the text.
            setTtsListener();
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"MessageId");
            tts.speak(text, TextToSpeech.QUEUE_ADD, map);
        }
    private void setTtsListener()
        {
        final SpeechRecognizer callWithResult = this;

        int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener()
        {
        @Override
        public void onDone(String utteranceId)
        {
        callWithResult.onDone(utteranceId);
        }
        @Override
        public void onError(String utteranceId)
        {
        callWithResult.onError(utteranceId);
        }
        @Override
        public void onStart(String utteranceId)
        {
        callWithResult.onStart(utteranceId);
        }
        });
        if (listenerResult != TextToSpeech.SUCCESS)
        {
        Log.e(TAG, "failed to add utterance progress listener");
        }


        }
         public void onDone(String utteranceId)
         {
             callSpeechRecognition();
         }
         public void onError(String utteranceId)
         {
         }
         public void onStart(String utteranceId)
         {
         }

TextToSpeech.SUCCESS 返回 0。

最佳答案

这是您的脚本的修改版本,其中调用了话语监听器中的 onDone() 方法。我已经删除了许多不直接连接到 TTS 播放的功能,以创建一个准系统示例。如果这对您有用,那么您可以将其他功能一一添加回去,测试过程中是否有任何问题。

import android.app.Activity;
import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.view.View;

import java.util.HashMap;
import java.util.Locale;


public class MainActivity extends Activity implements TextToSpeech.OnInitListener {

    private TextToSpeech tts;
    private SpeechRecognizer sr;

    private boolean initialized;
    private String queuedText;
    private String TAG = "TTS";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speech_recognizer);
        tts = new TextToSpeech(this /* context */, this /* listener */);
        tts.setOnUtteranceProgressListener(mProgressListener);

        sr = SpeechRecognizer.createSpeechRecognizer(this); // added
    }

    // Modified for testing purposes

    //This is called after first time user clicks a button
    /*
    private void processEnquiry() {
        for (int i = 0; i < EnquiryList.size(); i++) {
            speak(EnquiryList.get(i).toString());
        }

    }
    */

    public void processEnquiry(View v) {
        speak("Process enquiry");
    }
    // End of modification

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            initialized = true;
            tts.setLanguage(Locale.ENGLISH);

            if (queuedText != null) {
                speak(queuedText);
            }
        }
    }

    public void speak(String text) {
        // If not yet initialized, queue up the text.
        if (!initialized) {
            queuedText = text;
            return;
        }
        queuedText = null;
        // Before speaking the current text, stop any ongoing speech.
        //tts.stop();
        // Speak the text.
        setTtsListener(); // no longer creates a new UtteranceProgressListener each time
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "MessageId");
        tts.speak(text, TextToSpeech.QUEUE_ADD, map);
    }

    private void setTtsListener() {
        // Method radically simplified; callWithResult is retained but not used here
        final SpeechRecognizer callWithResult = sr; // was `this`
    }

    private UtteranceProgressListener mProgressListener = new UtteranceProgressListener() {
        @Override
        public void onStart(String utteranceId) {
        } // Do nothing

        @Override
        public void onError(String utteranceId) {
        } // Do nothing.

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

    private void callSpeechRecognition() {
        Log.d(TAG, "callSpeechRecognition() called");
    } 
}

关于android - 即使在传递 HashMap 参数后也不会调用话语进度监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26708828/

相关文章:

android - 为具有现有应用程序的设备发现特定的蓝牙设备

python - pyttsx 引擎 runAndWait 在 Yosemite 上不返回的解决方法

ios - 应用程序处于后台模式时的文本转语音功能?

android - 如何在应用程序启动时获取当前的 Facebook 访问 token ?

android - 在服务中获取实时数据库引用时出错

java - 如何在 android 模拟器中启用麦克风输入

algorithm - 为什么语音识别困难?

c# - 帮助 SAPI v5.1 SpeechRecognitionEngine 总是给出与 C# 相同的错误结果

android - 通过蓝牙的文本到语音在开始时被切断。如何解决?

android - 如何启动 com.android.launcher?