当前位置: 动力学知识库 > 问答 > 编程问答 >

media player - Android - Getting more than 2 OnCompleteListener events simultaneously from MediaPlayer

问题描述:

Hey guys I have come across an issue when using the MediaPlayer class in Android

Before you suggest SoundPool, I need an OnCompletion event to fire when the sound clip is done playing otherwise I would use it.

So the problem is is that when 3 or more clips are activated at the same time I only get the OnCompletion for the last 2 events, not all of them, anyone know a solution to this problem or know why it is happening?

// When the user clicks on the Chicken Image, this Function is called

public void onChickenClicked(View view)

{

// ToastMsg( "You Clicked the Chicken", Toast.LENGTH_SHORT );

// Then play the sound,

mMediaPlayer[CHICKEN] = MediaPlayer.create(MainActivity.this, R.raw.chicken_sound );

// Start playing the sound

mMediaPlayer[CHICKEN].start();

// This is to catch when the sound clip has ended, this will be

// used to stop the animation for the chicken

mMediaPlayer[CHICKEN].setOnCompletionListener( new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

System.err.println("Chicken On Complete");

// Stop the animation of the Chicken here

mAnimation.cancel();

mAnimation.reset();

// Then switch the image back to the original Chicken pic

ImageView imgView = (ImageView) findViewById(R.id.imageViewofChicken);

imgView.setImageResource(R.drawable.chicken);

mMediaPlayer[CHICKEN].reset();

}} );

// Finds the ImageView, replaces the base img with the alternate img, and plays the sound

animatePicture( R.id.imageViewofChicken, R.drawable.chicken_tongue);

}

public void onCowClicked(View view)

{

// ToastMsg( "You Clicked the Cow", Toast.LENGTH_SHORT );

// Then play the sound,

mMediaPlayer[COW] = MediaPlayer.create(MainActivity.this, R.raw.cow_sound );

// Start playing the sound

mMediaPlayer[COW].start();

// This is to catch when the sound clip has ended, this will be

// used to stop the animation for the chicken

mMediaPlayer[COW].setOnCompletionListener( new OnCompletionListener() {

public void onCompletion(MediaPlayer mp) {

System.err.println("Cow On Complete");

// Stop the animation of the Chicken here

mAnimation.cancel();

mAnimation.reset();

// Then switch the image back to the original Chicken pic

ImageView imgView = (ImageView) findViewById(R.id.imageViewofCow);

imgView.setImageResource(R.drawable.cow);

mMediaPlayer[COW].reset();

}} );

// Finds the ImageView, replaces the base img with the alternate img, and plays the sound

animatePicture( R.id.imageViewofCow, R.drawable.cow_tongue);

}

//there are more but you get it idea here.

Anyone have any ideas on how I can get all of the OnCompleteListeners to fire?

网友答案:

So I figured it out, I simply replaced the MediaPlayer with the SoundPool, and then used

private final ScheduledExecutorService mScheduler = Executors.newScheduledThreadPool(MAX_ANIMALS);

to handle when the sound clips were done with, based on a function I saw on stackoverflow using the MediaPlayer to load the sound, then get the duration in milliseconds, and use it as the delay for the scheduler (mScheduler[x]).

It's a hack, but it got it done.

分享给朋友:
您可能感兴趣的文章:
随机阅读: