我正在尝试将背景音乐应用到我正在开发的 Android 游戏中。当游戏进入音乐所在的 Activity 时,音乐会开始并继续播放。但是,当我的自定义 View 对象之一作为游戏的一部分被拖动时,音乐会停止。然后我尝试将媒体播放器放入一个线程中,但它的工作原理完全一样。你怎么看?
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());
...
最佳答案
看起来您的服务已启动NOT_STICKY
当操作系统垃圾收集时,它被杀死了!因此停止你的音乐。
无论您拖入的是什么,都会占用大量内存并调用 GC,如果您未绑定(bind)到您的服务,那么 GC 和清除的时机已经成熟。
也许看看:Bound Service
编辑更新问题
我会说,因为您稍后创建它 (mp),媒体播放器在 JVM 内存中较新,因此当 GC 出现时,它认为它是最近使用的并且不会处理它......只是还没有。稍后您可能会遇到原来的问题。
关于java - Android MediaPlayer 意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722941/