MediaPlayer 大约 5 秒后停止播放
我目前正在开发一个简单的游戏,现在是时候添加音乐和音效了。我尝试使用 MediaPlayer
,就像此处所述:Android 媒体播放器错误
但是我还有另一个问题,MediaPlayer
在大约 5 秒后停止播放音乐。可能是什么原因造成的?
I'm currently developing a simple game and now it's time to add music and sound effect. I tried using MediaPlayer
, just like described here: Android media player bug
However I have another problem, the MediaPlayer
stop playing the music after about 5 seconds. What is probable causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有这个问题。
这可能是由于 MediaPlayer 对象仅存在于方法中。
例如:
垃圾收集器很可能会介入并清除 MediaPlayer 对象。
这为我解决了错误:
I had this problem too.
It was probably due to having the MediaPlayer-object only existing within a method.
For example:
It is most likely that the garbage collector will come in and clean away the MediaPlayer-object.
This fixed the error for me:
就我而言,这只是因为在对话框中使用 MediaPlayer 并在播放名为
dismiss()
函数的声音后,垃圾收集器将消除 MediaPlayer 对象,并导致 MediaPlayer 停止工作,这是一种更好的方法在这种情况下播放声音是在父类中播放它而不是像这样在对话框中播放它In my case that was just because using MediaPlayer in a Dialog and right after playing the sound called
dismiss()
function therefore the garbage collector would eliminate the MediaPlayer object and it cause MediaPlayer stop working, a better way to play sound in such situation is to play it in the parent class instead of playing it in the Dialog like as this您应该创建一个异步代码,让媒体播放器播放他必须在后台播放的内容。像这样的东西:
You should create an asychronous code to let media player play what he has to play in the background. Something like this: