Prevent skipping or lag when adding music to an Android game

I recently added simple background music to my Android game. It uses Canvasto draw on the screen, so I am very careful in optimizing performance. I added music using the standard one MediaPlayer, which seems to be the most common and easiest way to convey melodies. Unfortunately, I have problems with music, skipping from time to time and, even worse, sometimes causing a significant lag in the game.

My only test phone is the Droid Eris, which is a pretty old phone, so I suspect this is not a problem on new Android phones, but I would like my game to be available on all Android devices. What can I do to add music while maintaining reasonable performance?

This 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();
+3
source share
2 answers

, . , Android, , , . OGG MP4, , ​​Android MediaPlayer ( , - OGG MP4), , . , , .

0

Source: https://habr.com/ru/post/1781891/


All Articles