Android MediaPlayer stopping unexpectedly
I am trying to implement background music into an Android game that I am working on. When the game gets to the activity that the music is in, the music starts and continues to play just fine. However, the music stops when one of my custom view objects is dragged as part of the game. I then tried putting the mediaplayer into a thread, but it worked exactly the same. What do you think?
if(Theme.getMusic()!=0){ //Theme.getMusic() returns a different resource depending on other settings.
MediaPlayer mp = MediaPlayer.create(GameView.this, Theme.getMusic());
mp.setLooping(true);
mp.start();
}
Logcat says this (last 6 lines seem relevant, not sure what to make of them):
04-19 18:13:54.440: INFO/MediaPlayer(10427): MediaPlayer create(context,resid)
04-19 18:13:54.440: INFO/MediaPlayer(10427): MediaPlayer
04-19 18:13:54.450: DEBUG/MediaPlayer(10427): hasHDMIPermission: -1
04-19 18:13:54.450: DEBUG/MediaPlayer(10427): registerHDMINotification
04-19 18:13:54.450: INFO/global(10427): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
04-19 18:13:54.460: ERROR/HDMIStatusObserver(10427): status: UNPLUG
04-19 18:13:54.460: DEBUG/MediaPlayer(10427): isHDMIPlug(): false
04-19 18:13:54.460: ERROR/MediaPlayer(10427): onHDMIStateChanged
04-19 18:13:54.460: ERROR/MediaPlayer(10427): hdmi_setting not found!
04-19 18:13:54.460: DEBUG/MediaPlayer(10427): getHDMIResolution: 00
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): Client(123) constructor
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): Create new client(123) from pid 10427, fd=23, offset=1090368, length=353892
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): setDataSource fd=23, offset=1090368, length=353892
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): st_dev = 7942
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): st_mode = 33188
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): st_uid = 1000
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): st_gid = 1000
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): st_size = 2512306
04-19 18:13:54.480: VERBOSE/StaticFunction(64): We only support url check function now.
04-19 18:13:54.480: VERBOSE/StaticFunction(64): isQCPFileFormat() url=(nul开发者_开发问答l)
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): player type = 3
04-19 18:13:54.480: DEBUG/MediaPlayerService(64): player type = VORBIS_PLAYER
04-19 18:13:54.480: VERBOSE/MediaPlayerService(64): create VorbisPlayer
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] setAudioStreamType(3)
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] prepareAsync
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] notify (0x35748, 1, 0, 0)
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] setLooping(1)
04-19 18:13:54.520: INFO/MediaPlayer(10427): MediaPlayer start()
04-19 18:13:54.520: INFO/MediaPlayer(10427): MediaPlayer invoke()
04-19 18:13:54.520: DEBUG/MediaPlayer(10427): setHDMIResolution: 0 0
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] setLooping(1)
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] setVolume(1.000000, 1.000000)
04-19 18:13:54.520: VERBOSE/AudioSink(64): setVolume(1.000000, 1.000000)
04-19 18:13:54.520: VERBOSE/MediaPlayerService(64): [123] start
04-19 18:13:54.520: VERBOSE/AudioSink(64): open(16000, 1, 1, 4)
04-19 18:13:54.531: VERBOSE/AudioSink(64): setVolume
04-19 18:13:54.531: VERBOSE/AudioSink(64): start
04-19 18:13:54.531: DEBUG/AudioPolicyManagerBase(64): startOutput() output 1, stream 3
04-19 18:13:54.541: DEBUG/AudioHardwareQSD(64): Enable ALT for speaker
04-19 18:13:54.541: DEBUG/AudioHardwareQSD(64): ALT batt temp = 318
04-19 18:13:54.580: INFO/MediaPlayer(10427): MediaPlayer handleMessage what=1
04-19 18:13:54.710: INFO/AudioHardwareQSD(64): AUDIO_START: start kernel pcm_out driver.
04-19 18:13:54.720: WARN/AudioFlinger(64): write blocked for 177 msecs, 59 delayed writes, thread 0x155b0
04-19 18:13:54.760: INFO/ActivityManager(107): Displayed activity com.detour.obstruction/.GameView: 398 ms (total 398 ms)
04-19 18:13:56.081: DEBUG/dalvikvm(10427): GC_EXTERNAL_ALLOC freed 1097 objects / 87248 bytes in 33ms
04-19 18:13:56.081: VERBOSE/MediaPlayerService(64): disconnect(123) from pid 10427
04-19 18:13:56.110: VERBOSE/MediaPlayerService(64): Client(123) destructor pid = 10427
04-19 18:13:56.110: VERBOSE/AudioSink(64): close
04-19 18:13:56.110: VERBOSE/MediaPlayerService(64): disconnect(123) from pid 10427
04-19 18:13:56.430: DEBUG/dalvikvm(10427): GC_EXTERNAL_ALLOC freed 314 objects / 18920 bytes in 31ms
Update: I declared my MediaPlayer as a field and then called create() later. This, for some reason, has fixed my problem! Although, I would still like to know what my problem was in the first place. Here's what I have now:
MediaPlayer mp;
...
mp = MediaPlayer.create(GameView.this, Theme.getMusic());
...
Looks like your Service started NOT_STICKY
and when the OS garbage collected it got killed! therefore stopping your music.
Whatever your dragging in is taking a lot of memory up and a GC is being called, if you aren't bound to your service, it's ripe to be GC'd and cleared.
Maybe look at: Bound Service
EDIT for updated Question
I would say, because your creating it (mp) later the mediaplayer is newer in the JVM memory and therefore when the GC comes along it see's it as used more recently and doesnt dispose of it...just yet. You may encounter the original problem later on.
精彩评论