安卓媒体播放器
我正在尝试使用 MediaPlayer 对象播放声音,但尽管我尽了最大努力,但似乎无法让它工作。声音只是拒绝播放。
这是一个短的声音,应该在触摸屏幕时播放,这意味着它必须重复很多次,而不会造成太多延迟。知道这一点后,我遵循了状态图, http://developer.android.com/reference /android/media/MediaPlayer.html。我似乎看不出我的方法调用顺序到底出了什么问题。
MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
mp.prepare();
mp.start();
Log.e("debug","sound played");
}
catch(Exception e) {}
mp.stop();
I am attempting to play a sound using a MediaPlayer object, but I cannot seem to get it to work despite my best efforts. The sound simply refuses to play.
It's a short sound, that is supposed to be played when the screen is touched, meaning it will have to be repeated many times without too much delay. Knowing this I followed the state diagram, http://developer.android.com/reference/android/media/MediaPlayer.html. I can't seem to see what exactly is wrong with my sequencing of method calls.
MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
mp.prepare();
mp.start();
Log.e("debug","sound played");
}
catch(Exception e) {}
mp.stop();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在媒体播放器上调用
prepare()
,但是 create() 您使用的调用会自动准备播放器,当您尝试调用prepare()< 时,这会导致
IllegalStateException
/代码>再次,您将被发送到您的catch()
(如果您以某种方式处理异常,即打印堆栈跟踪,您会注意到这一点)。You call
prepare()
on the mediaplayer, but the create() call you use prepares the player automatically, this causes anIllegalStateException
when you try to callprepare()
again, and you are sent to yourcatch()
(you would have noticed this if you handled the exception in some way, i.e. printing the stack trace).我们通过设置音乐播放器的一些属性来配置音乐播放器,如下所示
we configure the music player by setting some of its properties as shown below