H.Brooks H.Brooks - 4 months ago 12
Android Question

Android - play different sound on ever second click

I know when I want to play a sound when a user clicks a button its:

Button one = (Button) this.findViewById(R.id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho); one.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
mp.start();
} });


But what do I do when I have 2 sounds and I want to play a diffrent sound on every second click?

Answer
int iClicked = 0;
public void onClick(View v) 
{ 
    iClicked++;
    if(iClicked % 2==0){
       // Do sth, e.g. play sound I
    }
    else { // Do sth else, e.g. play sound II
    }
}
Comments