TextToSpeech的UtteranceProgressListener触发问题
2016-03-18 21:00
381 查看
最近在写一个安卓语音识别的项目,结果助教要求将其中的dialog变成voice dialog,也就是首先要把dialog的内容读出来,然后让用户说"yes"或者"no"触发相应的操作。本来不是很难,只需要在tts的callback中的onDone方法触发新的语音识别即可,但是遇到了一点小问题让我debug了两天。
首先上代码,这是tts的类
结果发现,尽管"this is the text to speech test"确实能读出来,但是onDone方法里的Toast并没有弹出。
原因是,speak方法有四个参数
再看UtteranceProgressListener的说明:
Listener for events relating to the progress of an utterance through the synthesis queue. Each utterance is associated with a call to speak(CharSequence, int, Bundle, String) or synthesizeToFile(CharSequence, Bundle, File, String) with an associated utterance
identifier, as per KEY_PARAM_UTTERANCE_ID. The callbacks specified in this method can be called from multiple threads.
这表示UtteranceProgressListener需要一个id,但我在使用speak的时候第四个参数设置成了null,导致UtteranceProgressListener没法触发,所以把代码
理论上应该没问题了。为啥是理论上呢?因为这样改完Toast依然没有触发。继续修改,将onDone方法改为如下:
首先上代码,这是tts的类
public class SpeechForDialog { TextToSpeech tts; protected Context context; protected String words=""; public SpeechForDialog(Context c,String s){ this.context=c; this.words=s; initSpeech(); } private void initSpeech(){ tts=new TextToSpeech(context.getApplicationContext(), new ttsInitListener()); } private class ttsInitListener implements TextToSpeech.OnInitListener { @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { if (tts!=null&&!tts.isSpeaking()) { CharSequence cs=words; tts.setLanguage(Locale.US); tts.setOnUtteranceProgressListener(new ttsUtteranceListener()); tts.speak(cs, TextToSpeech.QUEUE_FLUSH, null, null); } } else if(status == TextToSpeech.ERROR){ tts = null; Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show(); } } } private class ttsUtteranceListener extends UtteranceProgressListener{ @Override public void onStart(String utteranceId) { } @Override public void onDone(String utteranceId) { Toast.makeText(a, "toast", Toast.LENGTH_SHORT).show(); } @Override public void onError(String utteranceId) { } } }然后在MainActivity里使用 :
new SpeechForDialog(this,"this is the text to speech test");
结果发现,尽管"this is the text to speech test"确实能读出来,但是onDone方法里的Toast并没有弹出。
原因是,speak方法有四个参数
public int speak (CharSequence text, int queueMode, Bundle params, String utteranceId)
再看UtteranceProgressListener的说明:
Listener for events relating to the progress of an utterance through the synthesis queue. Each utterance is associated with a call to speak(CharSequence, int, Bundle, String) or synthesizeToFile(CharSequence, Bundle, File, String) with an associated utterance
identifier, as per KEY_PARAM_UTTERANCE_ID. The callbacks specified in this method can be called from multiple threads.
这表示UtteranceProgressListener需要一个id,但我在使用speak的时候第四个参数设置成了null,导致UtteranceProgressListener没法触发,所以把代码
tts.speak(cs, TextToSpeech.QUEUE_FLUSH, null, null);修改为
tts.speak(cs, TextToSpeech.QUEUE_FLUSH, null, "UniqueID");
理论上应该没问题了。为啥是理论上呢?因为这样改完Toast依然没有触发。继续修改,将onDone方法改为如下:
@Override public void onDone(String utteranceId) { final Activity a=(Activity)context; a.runOnUiThread(new Runnable() { public void 4000 run() { Toast.makeText(a, "kkkk", Toast.LENGTH_SHORT).show(); } }); }关于toast和thread的关系就不在这里细说了,总之修改完成后,toast就可以触发了!
相关文章推荐
- freemarker获取list<Javabean>的值
- nyoj325 zb的生日
- 简单脱壳教程笔记(3)---手脱UPX壳(2)
- 105. Construct Binary Tree from Preorder and Inorder Traversal | Java最短代码实现
- 初识ASP.NET Core 1.0
- json转bean对象
- 基于分治策略的排序算法:合并排序和快速排序
- 软件开发流程
- 263. Ugly Number
- 有返回结果的页面跳转
- Hush Framework 作为PHP服务端框架
- HDOJ 2014 青年歌手大奖赛_评委会打分
- Install Ubuntu 15.10, cmake, Emacs, newclisp, clang++, cppcms(DO NOT UPDATE Python!!!)
- 3. 变量分类
- Remove Duplicates from Sorted List
- 康托展开(方便排列计数)
- Mybatis中$和#的区别
- 使用汇编代码调用系统调用
- 八皇后问题
- IT十八掌作业_java基础第十二天_集合