这是我的代码,我有一系列的问题会被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.
@R_404_6120@:
这是脚本的修改版本,其中调用了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"); } }
总结 以上是内存溢出为你收集整理的android-即使传递了哈希映射参数,Utterance进度侦听器也不会被调用全部内容,希望文章能够帮你解决android-即使传递了哈希映射参数,Utterance进度侦听器也不会被调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)