其他分享
首页 > 其他分享> > android-即使传递了哈希映射参数,Utterance进度侦听器也不会被调用

android-即使传递了哈希映射参数,Utterance进度侦听器也不会被调用

作者:互联网

这是我的代码,我有一系列的问题会被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 one rror(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 one rror(String utteranceId)
         {
         }
         public void onStart(String utteranceId)
         {
         }

TextToSpeech.SUCCESS返回0.

解决方法:

这是脚本的修改版本,其中调用了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 one rror(String utteranceId) {
        } // Do nothing.

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

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

标签:speech-recognition,text-to-speech,android
来源: https://codeday.me/bug/20191121/2049140.html