即使在传递哈希映射参数之后,也不会调用话语进度监听器

时间:2014-11-03 06:20:15

标签: 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。

2 个答案:

答案 0 :(得分:2)

以下是脚本的修改版本,其中调用了Utterance侦听器中的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");
    } 
}

答案 1 :(得分:1)

我不确定这个答案是否会对你有所帮助,但我认为你不应该在每次请求TTS发言时都设置UtteranceProgressListener,而应该在onInit()上设置一次侦听器。 请注意,不会说出空文本,因此也不会调用回调。

虽然在TTS初始化之后基本上设置监听器对我来说很好并且在我的Nexus5和GalaxyS4上没有问题,即使每次我请求TTS说话时设置监听器,所以可能存在一些设备特定问题或某些TTS引擎具体问题。

糟糕,我忘了提及UptranceProgressListener在API级别15及更高版本上可用,因此不会在API级别14及更低版本上调用侦听器。