HB.
HB.

Reputation: 4226

Android - play different sound on ever second click

If I want to play a sound when a user clicks a button it is:

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?

Upvotes: 4

Views: 706

Answers (10)

Linh
Linh

Reputation: 60923

If you just want switch between 2 sounds, I think you don't need a boolean or int value for check first time Just check if sound1 is playing -> stop sound1 and open sound2 and opposite

final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.soho);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound);

buttonClick.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(mp1.isPlaying()){
             mp1.stop();
             mp2.start(); 
        }else{
             mp2.stop();
             mp1.start(); 
        }
    }
});

Hope this help

Upvotes: 0

selva
selva

Reputation: 1175

try something like

boolean playFirst = true;
 final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
        final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound_soho);
buttonClick.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        if(playFirst == true){
             mp.start(); 
            playFirst = false;
        }else{
             mp2.start(); 
            playFirst = true;
        }

    }
});

Upvotes: 0

shinil
shinil

Reputation: 1493

    int count=0;
    Button one = (Button) this.findViewById(R.id.button1);
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);

    one.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            count++;

            if(count%2==1) {
                //play first sound
            }else{
                //play second sound
            }

        } });

Upvotes: 0

Mikhail Spitsin
Mikhail Spitsin

Reputation: 2608

public class ButtonPlayer implements View.OnClickListener {
    private final int[] sounds;
    private int soundIndex = -1;

    public ButtonPlayer(int... sounds) {
        this.sounds = sounds;
    }

    @Override
    public void onClick(View view) {
        if (sounds.length != 0) {
            soundIndex++;
            soundIndex = soundIndex % sounds.length;
            MediaPlayer mp = MediaPlayer.create(this, sounds[soundIndex]);
            mp.start();
        }
    }
}

Then somewhere in your code:

Button one = (Button) this.findViewById(R.id.button1);
one.setOnClickListener(new ButtonPlayer(
                           R.raw.first_composition,
                           R.raw.second_composition));

You can optimize this class later, by generating 'n' media players for each sound in constructor. But it's is task of optimization, not of implementation.

And of course always try to create independent classes for even small features. Don't clutter up activity.

Upvotes: 0

Mohammed Elrashied
Mohammed Elrashied

Reputation: 332

you can put the reference to the audio clip into an array

int[] audio={R.raw.clip1,R.raw.clip2};
for(i=0;i<audio.length;i++){

//your MediaPlayer Code here

}

i didnt try this , but you will get the idea of how to play many clips. good luck

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33238

it's very simple. look at sample code and try...

        final boolean isFirstSound = true;
        Button one = (Button) this.findViewById(R.id.button1);
        final MediaPlayer mp = MediaPlayer.create(this, R.raw.soho);
        final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.second_sound);
        one.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                isFirstSound = !isFirstSound;
                if(isFirstSound)
                    mp.start();
                else
                    mp2.start();
            }
        });

Upvotes: 0

Sreehari
Sreehari

Reputation: 5655

MediaPlayer mp;    
int clickCount=0;
one.setOnClickListener(new OnClickListener(){ 
public void onClick(View v) 
{ 
 if(clickCount %2 ==0){
    mp = MediaPlayer.create(this, R.raw.soho);
 }
 else{
    mp = MediaPlayer.create(this, R.raw.another);
 }
 mp.start();
 clickCount++;
} });

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

I want to play a diffrent sound on every second click?

For this :

1. Store all raw file id's in an Integer Array.like:

int arrRawFiles ={R.raw.soho,R.raw.soho2,R.raw.soho3}

2. On onClick of Button generates a random integer according to Array length and then get raw file id from Array using that number:

  public void onClick(View v) 
   { 
     int index = new Random().nextInt(arrRawFiles.length);
     MediaPlayer mp = MediaPlayer.create(this,index);
     mp.start(); 
   }

Upvotes: 0

Cgx
Cgx

Reputation: 763

try this

    private int rId=0;//class member
    //onclick
    final int [] music={R.raw.soho,R.raw.other};
    int id=music.length%2;
    final MediaPlayer mp = MediaPlayer.create(this, rId);
    id++;

Upvotes: 0

David
David

Reputation: 16277

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
    }
}

Upvotes: 3

Related Questions