How to find out if sound is finished in android?

How do I know if a sound has finished playing?

I want to play 2 sounds, but I want one sound to play, and then wait until the first sound is made before the second.

Also, if I wanted to do something else when the sound is finished, how to show its next appearance in the flipper viewer, can I do this?

Right now I'm using SoundPool to play my sounds.

+6
android soundpool
source share
4 answers

Use the MediaPlayer class and OnCompletionListener

+3
source share

Just an idea ... get the ogg / wav play time length and use your own SoundObject class with a timer member. Update it in some update function. If timer> maxtime + threshold, mark it as ready.

0
source share

As I decided, this is to create an animation sequence containing the image, sound and delay. Then I can queue them so that I can show the image, and the sound accompanies it, and then switch to another image. I do this with SoundPlayer and a custom view (and not a flipper, although you probably could)

The trick is that you cannot just pause the thread for a delay, you will need to use a handler to send yourself messages to update the user interface in the future.

Here is the handler

handler = new Handler() { /* (non-Javadoc) * @see android.os.Handler#handleMessage(android.os.Message) */ @Override public void handleMessage(Message msg) { if (msg.what == NEXT_ITEM_MSG) { showNext(); } else if (msg.what == SEQUENCE_COMPLETE_MSG) { // notify the listener if (animationCompleteListener != null) { animationCompleteListener.onAnimationComplete(); } } } }; 

The showNext () method simply captures the next element in the sequence, updates the image, plays a sound, and then displays a message for the handler to call showNext after the sequence.

 private void showNext() { if (mRunning) { // Get the first item AnimatedSequenceItem item = currentSequence.getNextSequenceItem(); if (item == null) { Message msg = handler.obtainMessage(SEQUENCE_COMPLETE_MSG); handler.sendMessage(msg); return; } // Show the first image showImage(item.getImageResId()); // Play the sound int iSoundResId = item.getSoundResId(); if (iSoundResId != -1) { playSound(iSoundResId); } // schedule a message to advance to next item after duration Message msg = handler.obtainMessage(NEXT_ITEM_MSG); handler.sendMessageDelayed(msg, item.getDuration()); } } 

Of course, all this requires you to define and hard-code the delays, but, in my experience, you have to do this for the game anyway. I had many occasions when I wanted the delay to be longer than the sound. Here's a code that actually queues items.

 battleSequence.addSequenceItem(R.drawable.brigands, R.raw.beep, 2000); battleSequence.addSequenceItem(R.drawable.black, winning ? R.raw.enemy_hit : R.raw.player_hit, 1500); 

With this approach, I can have all kinds of logic for adding animation elements, changing durations, looping animations, etc. It just required some tweaking to get delays.

Hope this helps.

0
source share

Sorry it's too late to reply. But I used the isPlaying () API from the media planner to wait for the completion of the first song.

 while(mediaPlyaer.isPlaying()){ } 
0
source share

All Articles