I came up with a good multiple WAV playing trick.

All your problems with Audio, Video and Images.

I came up with a good multiple WAV playing trick.

Postby palisade » Sat Jan 17, 2009 8:52 pm

First set up your media player, I've found you can create 32 of these, I think of them like buffers or voices assigned to a specific sound input (though you can recreate them, just release first... also you can splice multiple sounds together into a single wav file and jump between their start/end times via seekTo if you need more voices than 32):

Code: Select all
mPlayer = MediaPlayer.create(this, R.raw.mysound);


Here's the useful part:

Code: Select all
    private void playWAV(MediaPlayer player) {
       if (player.isPlaying()) {
          if (player.getDuration() - player.getCurrentPosition() > 100)
             player.seekTo(0);
          return;
       }
       player.start();
    }


What this does is allow you to play a single sound once, but also allows you to play it again before it has finished. It asynchronously lets you start the sound again without any delays. Whereas, a stop() then start() crashes, and the proper way to stop/start is stop() then prepareAsync() then start(), however this is slow. Using the seekTo method above is fast, the buffer never stops playing until it finishes. When it does complete, you'll be able to invoke player.start() again and continue as normal. You could take out the condition, but it is there to allow you to tune how often you'll pop. You can adjust the 100ms in the duration condition to whatever you feel comfortable with.

Hope this helps someone.
palisade
Freshman
Freshman
 
Posts: 8
Joined: Thu Jan 08, 2009 11:24 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 8 guests