Tahmid
Tahmid

Reputation: 35

Android Media Player does not stop. When I click on a play button, it plays multiple times

When I click on the play button again and again, it plays multiple times simultaneously. I want to stop multiple playing. Here's the code:

Objects for Media player and buttons


MediaPlayer mPlayer;
Button playbtn;
Button stopbtn;

Button click event listener to play audio


playbtn = (Button) findViewById(R.id.play);
playbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

                Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
                mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                try {

                    mPlayer.setDataSource(getApplicationContext(), myUri1);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mPlayer.start();




Button click event listener to stop audio


stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(mPlayer!=null && mPlayer.isPlaying()){
            mPlayer.stop();
        }
    }
});

any help would be appreciated. Thanks!

Upvotes: 1

Views: 155

Answers (1)

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

It is creating a new player with every play click but only keeping the reference to the last player so the solution is to keep player in a list and stop all.

private List<MediaPlayer> players = new ArrayList<>()



stopbtn = (Button) findViewById(R.id.stop);
stopbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        for(player in players){
            if(player!=null && player.isPlaying()){
                mPlayer.stop();
            }
        }
        players.clear();
    }
});

Another solution is, only use one instance of player. move the player initialization outside click as:

playbtn = (Button) findViewById(R.id.play);

Uri myUri1 = Uri.parse("file:///sdcard/Birds/parrot.mp3");
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {

    mPlayer.setDataSource(getApplicationContext(), myUri1);
} catch (IllegalArgumentException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (SecurityException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    e.printStackTrace();
}


try {
    mPlayer.prepare();
} catch (IllegalStateException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
}
playbtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
            if(!mPlayer.isPlaying())
                mPlayer.start();

Upvotes: 2

Related Questions