开发者

Preventing skipping or lag when adding music to Android game

开发者 https://www.devze.com 2023-01-31 19:58 出处:网络
I recently added simple looping background music to my Android game. It uses Canvas to draw to the screen, so I am very careful about optimizing performance. I added music using the standard MediaPlay

I recently added simple looping background music to my Android game. It uses Canvas to draw to the screen, so I am very careful about optimizing performance. I added music using the standard MediaPlayer which seems to be the most common and easiest way to handing looping music. Unfortunately, I'm having trouble with the music skipping occasionally and, far worse, sometimes causing significant lag in the game play.

My only phone to test on is a Droid Eris, which is a fairly old phone, so I suspect this would not be an issue on newer Android phones, but I would like my game to be playable on all Android devices. What can I do to add music while maintaining reasonable performance?

T开发者_JAVA技巧his is the code I added:

// in setup() method of game thread
mediaPlayer = MediaPlayer.create(context, R.raw.rl_theme);
mediaPlayer.setLooping(true);
mediaPlayer.start();
// in method called when the player looses
mediaPlayer.stop();
mediaPlayer.release();


I have not ever had this issue, so I am not positive, but I think SoundPool is what you're looking for.

http://developer.android.com/reference/android/media/SoundPool.html


It looks like there isn't much you can do. Yes, adding music does slow Android apps down some, but it doesn't seem to be a major issue on newer phones. I did change the format of the audio files from OGG to MP4 to fix what appears to be a bug in the Android MediaPlayer (still looking into it, but the media server was crashing on OGG and works perfectly with MP4) and the performance seems to have gotten better. I'm not sure if that's related, though.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号