媒体播放器停止约5秒钟后打 [英] MediaPlayer stop playing after about 5 seconds
问题描述
我目前正在开发一个简单的游戏,现在是时候添加音乐和声音效果。我试图用的MediaPlayer
,就像这里描述:机器人媒体播放器问题
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 problem
不过,我还有一个问题,在的MediaPlayer
停止播放音乐约5秒钟后。什么是可能造成的?
However I have another problem, the MediaPlayer
stop playing the music after about 5 seconds. What is probable causing this?
推荐答案
我有这个问题了。 这可能是由于具有在MediaPlayer对象仅一方法在现有
I had this problem too. It was probably due to having the MediaPlayer-object only existing within a method.
例如:
//ERROR, stops after 5 sec!
public static void playMusic(int id)
{
MediaPlayer mediaPlayer = MediaPlayer.create(context, id);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
这是最有可能的是,垃圾收集器会在和清理掉的MediaPlayer对象。
It is most likely that the garbage collector will come in and clean away the MediaPlayer-object.
此固定的错误对我来说:
This fixed the error for me:
//mediaPlayer-object will not we cleaned away since someone holds a reference to it!
private static MediaPlayer mediaPlayer;
public static void playMusic(int id)
{
mediaPlayer = MediaPlayer.create(context, id);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
这篇关于媒体播放器停止约5秒钟后打的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!