Android-从MediaPlayer同时获取2个以上的OnCompleteListener事件

Android-从MediaPlayer同时获取2个以上的OnCompleteListener事件,android,media-player,Android,Media Player,嘿,伙计们,我在Android中使用MediaPlayer类时遇到了一个问题 在您建议SoundPool之前,我需要在播放完声音片段后触发OnCompletion事件,否则我会使用它 所以问题是,当3个或更多剪辑同时激活时,我只得到最后2个事件的OnCompletion,而不是所有事件,有人知道这个问题的解决方案或知道它发生的原因吗 // When the user clicks on the Chicken Image, this Function is called public void

嘿,伙计们,我在Android中使用MediaPlayer类时遇到了一个问题

在您建议SoundPool之前,我需要在播放完声音片段后触发OnCompletion事件,否则我会使用它

所以问题是,当3个或更多剪辑同时激活时,我只得到最后2个事件的OnCompletion,而不是所有事件,有人知道这个问题的解决方案或知道它发生的原因吗

// 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.

有人知道我如何让所有的OnCompleteListener都启动吗?

所以我想出来了,我只是用SoundPool替换了MediaPlayer,然后使用

private final ScheduledExecutorService mScheduler=Executors.newScheduledThreadPool(MAX_)

为了处理声音剪辑何时完成,根据我在stackoverflow上看到的一个函数,使用MediaPlayer加载声音,然后获取持续时间(毫秒),并将其用作调度程序的延迟(
mScheduler[x]


这是一个黑客程序,但它成功了。

所以我想我找到了答案,我只是用SoundPool替换了MediaPlayer,然后使用private final ScheduledExecutorService msScheduler=Executors.NewsScheduledThreadPool(MAX_);为了处理声音剪辑何时完成,根据我在stackoverflow上看到的使用MediaPlayer加载声音的函数,然后获取持续时间(毫秒),作为调度程序的延迟。private int getSoundClipDuration(int rawSoundId){//创建一个MediaPlayer来播放我们的声音MediaPlayer mp=MediaPlayer.Create(MainActivity.this,rawSoundId);if(mp==null){return-1;}int duration=mp.getDuration();//获取声音片段的长度mp.reset();mp.release();//释放MediaPlayer以供其他人使用返回持续时间;}