1

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.

すべてのOnCompleteListenersを起動する方法について誰かが何かアイデアがありますか?

4

1 に答える 1

0

だから私はそれを理解しました、私は単にMediaPlayerをSoundPoolに置き換えてから使用しました

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

MediaPlayerを使用してサウンドをロードするstackoverflowで見た関数に基づいて、サウンドクリップがいつ処理されたかを処理し、ミリ秒単位で期間を取得し、それをスケジューラーの遅延として使用します(mScheduler[x])。

それはハックですが、それは成し遂げられました。

于 2014-09-22T23:35:57.037 に答える