Skip to content Skip to sidebar Skip to footer

Problems With Media Player

Alright so, I am making sound application for android and there is a slight problem. I made my app to stop playing current sound when I click on another button to play another soun

Solution 1:

This time, the problem is with your order of checking the media players and logical if else conflict.

Make use of this code in your mp2 part:

 mp2=MediaPlayer.create(this, R.raw.famas);
 ImageButtonbtn2= (ImageButton) findViewById(R.id.btn2);
 btn2.setOnClickListener(newView.OnClickListener() {

@OverridepublicvoidonClick(View v) {

    if (mp.isPlaying()){
        mp.pause();
        mp.seekTo(0);
    }
    if (mp3.isPlaying()){
        mp3.pause();
        mp3.seekTo(0);
    }
    if (mp4.isPlaying()){
        mp4.pause();
        mp4.seekTo(0);
    }
    if (mp5.isPlaying()){
        mp5.pause();
        mp5.seekTo(0);

    }
    if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }
     if (mp2.isPlaying()){ // Put this if branch just before else, likewise for other media players too.
        mp2.pause();
        mp2.seekTo(0);
    } 
    else{
        mp2.start();
    }
    }

});

Always check if mp2 is playing or not just before the else condition. Likewise for others. That is, only after do you check other players, you should check the current player you are trying to play.

Let me explain why in detail:

 if (mp6.isPlaying()){
        mp6.pause();
        mp6.seekTo(0);
    }

    else{
        mp2.start();
    }
    }

In that portion of your code in mp2 part, you are checking if mp6 is playing and stopping it. Since if holds good, elsewon't execute in that turn. Hence you have to press again to make mp2 play. By doing as I said, that is, checking the current player will be matched with the corresponding else condition, and hence your problem will be solved.

EDIT: In all six button click events, you have checked if mp6 is playing just before the else part. This is a logical flaw. Before the else, you should have the if of the corresponding mediaplayer object.

Post a Comment for "Problems With Media Player"