Android MediaPlayer意外停止

时间:2011-04-19 21:29:34

标签: java android

我正在尝试将背景音乐应用到我正在制作的Android游戏中。当游戏进入音乐所在的活动时,音乐开始并继续播放就好了。但是,当我的一个自定义视图对象作为游戏的一部分被拖动时,音乐会停止。然后我尝试将媒体播放器放入一个线程中,但它的工作方式完全相同。你觉得怎么样?

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说这个(最后6行看似相关,不知道该怎么做):

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=(null)
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

更新:我将MediaPlayer声明为字段,然后稍后调用create()。由于某种原因,这已经解决了我的问题!虽然,我仍然想知道我的问题是什么。这就是我现在所拥有的:

MediaPlayer mp;
...
mp = MediaPlayer.create(GameView.this, Theme.getMusic());
...

1 个答案:

答案 0 :(得分:2)

您的服务似乎已启动NOT_STICKY

当操作系统垃圾收集时它就被杀了!因此,停止你的音乐。

无论你拖入什么是占用大量内存并且正在调用GC,如果你不受你的服务约束,那么GC和清除它已经成熟。

也许看看:Bound Service

编辑更新的问题

我想说,因为你创建它(mp)以后媒体播放器在JVM内存中是更新的,因此当GC出现时它会看到它最近使用并且没有处理它......就在这里。您可能会在以后遇到原始问题。