简体   繁体   English

MediaPlayer播放完毕后执行代码吗?

[英]Executing code after MediaPlayer is done playing?

I was wondering I'm working on an Android app where MediaPlayer/TextToSpeech is used to tell the user something and then a Speech Recognition object opens up after the MediaPlayer/TextToSpeech finishes and another is played after they answer the question with the recognition. 我想知道我正在开发一个Android应用程序,其中使用MediaPlayer / TextToSpeech告诉用户一些信息,然后在MediaPlayer / TextToSpeech完成后打开语音识别对象,并在他们回答识别问题后播放另一个语音识别对象。 Problem is I'm not 100% sure of a way, or good way, to delay the Speech Recognition from opening right after MediaPlayer is starts, or waiting till mediaplayer is done before speech recognition opens up. 问题是我不确定在MediaPlayer启动后立即打开语音识别,还是等到Mediaplayer完成后才打开语音识别,才能够百分百确定是否有办法延迟语音识别。 I see a few options based off my own knowledge/Google - 我根据自己的知识/ Google看到了一些选择-

use the methods isPlaying(MediaPlayer)/isSpeaking(TextToSpeech) and putting them in some kind of loop to check until they aren't true anymore, then opening the speech recognition. 使用方法isPlaying(MediaPlayer)/ isSpeaking(TextToSpeech)并将它们放入某种循环中进行检查,直到不再正确为止,然后打开语音识别。

Call Thread.sleep(). 调用Thread.sleep()。 The problem I see with this is I'm not sure exactly how long user is going to speak so it could still get interrupted unless I put it at a high time which just slows the app down. 我看到的问题是,我不确定用户要说多长时间,因此除非我将其放在较高的时间(这只会降低应用的运行速度),否则它仍然会被打断。

I've research and played around with the Handler class a bit, but haven't fully comprehended how to do this...anyone help? 我已经研究过并使用Handler类,但还没有完全理解该怎么做...有人帮忙吗?

如果要在MediaPlayer完成时执行某些操作,则可以使用setOnCompletionListener()设置OnCompletionListener ,以在媒体播放完成时接收回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM