1

I write Android game where one unit has 3 audio stages:

  • get hit
  • attack
  • dead

So I added to my Unit Object followed code:

    MediaPlayer mMediaPlayerGetHit = null;
    MediaPlayer mMediaPlayerDead = null;
    MediaPlayer mMediaPlayerAttack = null;

In constructor:

  mMediaPlayerAttack=MediaPlayer.create(context,R.raw.fly_monster_attack1);
  mMediaPlayerDead=MediaPlayer.create(context,R.raw.fly_monster_death1);
  mMediaPlayerGetHit=MediaPlayer.create(context,R.raw.fly_monster_gethit1);

  mMediaPlayerAttack.setOnPreparedListener(this);
  mMediaPlayerDead.setOnPreparedListener(this);
  mMediaPlayerGetHit.setOnPreparedListener(this);

  ....

Now it's seem messy,

Hmm, 3 player objects, one per unit state.

I'm sure its wrong way and I need to change each time MediaPlayer source regarding to unit state,

But here comes performance problem:

Lets say my MediaPlayer has took sources attack.

After, I switch it to get hit, after once more to attack.

Maybe I need to save instance of MediaPlayer with attack somehow and reuse it?

I don't see that its good way hundreds times switch my poor MediaPlayer.

Any Ideas?

Thank you,

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225

2 Answers2

5

You should take a look at SoundPool class, i think is way better suited for what you are trying to do, here is a good example of how to use it:

http://www.edumobile.org/android/android-programming-tutorials/sound-pool-example-in-android-development/

SoundPool is perfectly suited for games, i've used it in mine and works perfect, the class allows you to, easily loop, play, and stop several sounds simultaneously, without creating one MediaPlayer per action, all you need to worrie about is to release the pool when you are done with it. Also depending on what you are doing it has a very efficient priority system, that allows you to set a max amount of sounds to play and based on priority let another sounds continue playing and taking resources of another sounds when inserting new ones...

Documentation:

Priority runs low to high, i.e. higher numbers are higher priority. Priority is used when a call to play() would cause the number of active streams to exceed the value established by the maxStreams parameter when the SoundPool was created. In this case, the stream allocator will stop the lowest priority stream. If there are multiple streams with the same low priority, it will choose the oldest stream to stop. In the case where the priority of the new stream is lower than all the active streams, the new sound will not play and the play() function will return a streamID of zero.

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
5

For short sound bites like those, you should really look into using SoundPool instead of MediaPlayer, although it uses MP to decode the audio..it's designed for low latency streams, and allows multiple streams at the same time.

In addition to low-latency playback, SoundPool can also manage the number of audio streams being rendered at once. When the SoundPool object is constructed, the maxStreams parameter sets the maximum number of streams that can be played at a time from this single SoundPool. SoundPool tracks the number of active streams. If the maximum number of streams is exceeded, SoundPool will automatically stop a previously playing stream based first on priority and then by age within that priority. Limiting the maximum number of streams helps to cap CPU loading and reducing the likelihood that audio mixing will impact visuals or UI performance.

So, for something like a game where multiple sounds need to be rendered at once, SoundPool may be ideal.

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41
  • Thank you mate, Once more StackOverflow helps me and guys like you ;) – Maxim Shoustin Jul 11 '13 at 19:01
  • I ran into some restrictions with SoundPool. According to other answers it can only play filed (http://stackoverflow.com/questions/7428448/android-soundpool-heapsize-overflow) and has no listener on playback completion (http://stackoverflow.com/questions/18567242/how-to-know-when-soundpool-finished-playing-audiofile). It seems that SoundPool is not automatically the preferred solution. – Reinhold May 08 '15 at 13:08