Media player object blues...
Carsten Schw...
4 posts
|
Hi I've made a little game where you can remove stuff that appears on the screen of your phone. I haved added sound by creating four mediaplayers.
I started using the sounds in the game and it worked fine, then i added the option of playing music in the background, I enable/disable music based on the settings in the surfaceCreated method of my view:
The methods for playing sounds and music looks like this:
There are some issues with this:
Any comments on these? |
Ed Burnette
357 posts
|
You can’t have very many of them. Android doesn’t tell you what the limit is but it seems to be around 4. You’re probably getting a null returned from the MediaPlayer.create sometimes. I recommend the following best practices: - Don’t create your media players in the constructor. If you do it there, then there’s no place for you to call player.release, which is very important or you’ll run out of resources. You could create them in onSurfaceCreated and release them in onSurfaceDestroyed, but as a best practice I think it’s better if you do anything not related to the graphics in your Activity instead of your View. So, do it in onResume and onPause in your Activity. - Likewise, start your thread in on(Something)Created or onResume and stop it in on(Something)Destroyed or onPause. Do everything that you create or initialize in matched bookend pairs like this. - If you want overlapping sounds, and sounds that happen instantly because of some event like a button press or a ball hitting a wall, then use SoundPool instead of MediaPlayer. Also use .ogg format because it’s quicker to decode and play without pauses. Hope this helps, and good luck. |
2 posts, 2 voices
