-1

I want to play two different sound file when the user clicks the button

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        playSound(this,R.raw.s1);
        playSound(this,R.raw.s2);

    }

    public static void playSound(Context context, int soundID){      
        MediaPlayer mp = MediaPlayer.create(context, soundID); 
        mp.start();
        }

the problem on this code is the s1 and s2 files are played on the same time , I want to play s1 then if s1 finsh playing s2 file, How can I do that?

4

2 回答 2

0

Try this:

int[] sounds = new int[] {R.raw.s1, R.raw.s2};    
int counter = 0;

@Override
public void onClick(View v) {
    playSound(sounds[counter]);
}

public void playSound(Context context, int soundID){      
    MediaPlayer mp = MediaPlayer.create(context, soundID); 
    mp.setOnCompletionListener(this);
    mp.start();
}

@Override
public void onCompletion(MediaPlayer mp) {
    counter++;
    if (counter < sounds.length) {   
        playSound(sounds[counter]);
    }
}

Not the most elegant thing I've ever written but it should fulfill your requirement.

于 2013-06-17T16:33:58.277 回答
0

Try this:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    MediaPlayer mp = MediaPlayer.create(context,R.raw.s1); 
    mp.start();
    mp.setOnCompletionListener(new OnCompletionListener() 
    {
        public void onCompletion(MediaPlayer mp2){   

            mp2 = MediaPlayer.create(context,R.raw.s2); 
            mp2.start();
        }
    });
}
于 2013-06-17T17:39:18.030 回答