Reputation: 33
I am making a game for android which needs to play a small beep whenever a user touches a certain object.
To do this I have this simple code to create the media player. beep is a small .ogg
file of ~1.5 sec length.
MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.beep);
mp.setVolume(0.5f, 0.5f);
and use it like this
mp.start();
The problem I am having is that my game uses the android canvas, and the thread which draws to the canvas also calls mp.start()
. I have discovered that playing the sound is taking quite a lot of time, and it is very varied in how long it takes. Sometimes 1ms, sometimes 15ms. This appears to be causing my game to noticeably lag a little bit, as the android canvas is pretty slow anyway.
I've had a look at the logs and I don't appear to having garbage collection causing the delay. I'm wondering if anyone can give me some advice to reduce this delay
EDIT - I've just discovered that MediaPlayer
is designed for longer sound files, and SoundPool
is better for shorter audio files. However, I load the file into the mediaPlayer
only once, so I don't see how it should make any difference. I will test it out though
Upvotes: 3
Views: 3881
Reputation: 315
sorry for my english
Use SoundPool instead of MediaPlayer cause MediaPlayer need releasing memory after each call or call method mp.release()
after each call of player
Upvotes: 1