Issue
Or do I constantly have to reinstantiate it?
I have a sound file that plays an explosion every time a ship blows up, but sometimes three or more ships can blow up at the same time, so I want the same sound clip to overlap. I notice it won't do that unless I create new instances. Is there something I'm missing or is that the only way to do it?
//declaration
private final MediaPlayer[] explosionSound = new MediaPlayer[5];
//constructor
for (int i = 0; i < 5; i++)
{
explosionSound[i] = MediaPlayer.create(app, R.raw.explosion);
}
//in program where I want to use it (when I want to play the sound)
for (int i = 0; i < 5; i++)
{
if (!explosionSound[i].isPlaying())
{
explosionSound[i].start();
break;
}
}
//onDestroy() method to release the media
for (int i = 0; i < 5; i++)
{
explosionSound[i].release();
}
I tried to instantiate a new one anonymously but it complained about it not being released (the error goes away when you release it in the onDestroy() method). I can't release it right away or else the sound doesn't play at all. I don't want to limit myself to 5 explosions either. Now I'm worried about memory issues too.
Is there an easier way to do this?
Solution
MediaPlayer is not really meant to solve the problem of small, overlapping audio clips. It's more for long-running media, both audio and video.
A better tool for your particular situation is SoundPool, which is specifically designed for your case.
Answered By - Doug Stevenson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.