Dmitry
Dmitry

Reputation: 1561

Android button click to play music, click again to stop sound

I have a button, when I click it plays music, how to do it, when I click second time, to stop the music?

 Button two = (Button)this.findViewById(R.id.button2);
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
            two.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {
                    mp2.start();
                }
            });  

Ok, this one works:

Button one = (Button)this.findViewById(R.id.button1);
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.n);
    one.setOnClickListener(new OnClickListener(){

        @Override
            public void onClick(View v) {
            if (mp1.isPlaying()) {
                mp2.pause();
                } 
                else {
                mp2.start();
            }
        ;
    }});

The one with Pause above it works, but If I want to stop the music, it does not work. Following not working:

@Override
        public void onClick(View v) {
        if (mp1.isPlaying()) {
            mp2.stop();
            } 
            else {
            mp2.start();
        }
    ;
}});

I get error: start called in state 0
error (-38, 0)

Upvotes: 1

Views: 35352

Answers (9)

Muhammad Akif Saleem
Muhammad Akif Saleem

Reputation: 13

If you have only one sound and you want it to start or stop with only one button than this may help .

 Button button_name = (Button)this.findViewById(R.id.Your_button_id_Here);
 final MediaPlayer mp = MediaPlayer.create(this, R.raw.YOur_audio_File_name_here);
 button_name.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {

                 if(mp.isPlaying())
                 {
                    mp.pause();
                 }
                 else 
                 {
                mp.start();
                 }
            }
        });

Upvotes: 1

Prashant Shubham
Prashant Shubham

Reputation: 516

Add a global variable boolean flag=false;

if(flag==false)
    {
        mp=MediaPlayer.create(this, R.raw.abc);
        mp.start();
        playbutton.setText("Pause");
        flag=true;
    }
    else if(mp.isPlaying()&&flag==true)
    {
        mp.pause();
        playbutton.setText("Play");
        flag=false;
    }

This code will work if you want to use the same button as PLAY/PAUSE in your app. Hope this helps. Add this code in the button onClick() function which you are using to play or pause.

Upvotes: 0

Mangiare
Mangiare

Reputation: 1

I solved this problem in a manual way. If sound is playing, i wrapped sound into beginning(seekTo(0)), then paused the sound. If sound is already paused, only seekTo(0) is called.

 public void onClick(View v) {
       if(mp.isPlaying()){
          mp.seekTo(0);
          mp.pause();
        }
        else{
            mp.seekTo(0);
        }
    }    ;

Upvotes: 0

aleksandar
aleksandar

Reputation: 110

I accomplished this as follows:

I declared a boolean variable:

boolean isButtonClicked = false;

Then I set up an if else in a method like so:

initPlaySound()
{
    if(isButtonClicked)
    {
       player.start();
    } else{
       player.stop();
}

Lastly I set up an onClick of the button within the method:

button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            isButtonClicked = !isButtonClicked;
            initPlaySound();
        }
    });

Basically, when the button is first clicked the boolean is set to the opposite of its' current state (false to true) and the code within the if executes. Once the button is clicked again the boolean is set to the opposite again (true to false) and the sound stops.

Upvotes: 0

mobeirn
mobeirn

Reputation: 43

This is what I used to start and stop the music, you must prepare the MediaPlayer for playback. Note: this will start the music from where it stopped.

Mediaplayer prepare() method

If you are streaming music you should use prepareAsync ()

musicButton = (Button) findViewById(R.id.btn_dj_player);
    musicButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mp.isPlaying()) {//check if a song is already playing
                mp.stop();
                try {
                    mp.prepare();//get the mediaplayer reeady for playback
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else {
                mp.start();
            }
        }   
    });

Upvotes: 1

Laurence Marshall
Laurence Marshall

Reputation: 27

Have you tried using the seekTo() function?

Something like:

if (mp.isPlaying()) {
            mp.stop();
            mp.prepareAsync();
            mp.seekTo(0);
        } else {
            mp.start();
        }
    }

I am just guessing here, but might be worth looking into. :-)

Upvotes: 0

David Neto
David Neto

Reputation: 808

According to http://developer.android.com/reference/android/media/MediaPlayer.html, I suppose you could do something like this:

 Button two = (Button)this.findViewById(R.id.button2);
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.two);
            two.setOnClickListener(new OnClickListener(){

                public void onClick(View v) {

                    // If the music is playing
                    if(mp2.isPlaying() == true)
                        // Pause the music player
                        mp2.pause();
                    // If it's not playing
                    else
                        // Resume the music player
                        mp2.start();
                }
            });

You can actually write just

 if(mp2.isPlaying())

instead of

 if(mp2.isPlaying() == true)

It's just for the sake of understanding what is going on.

Upvotes: 3

Exoit
Exoit

Reputation: 76

if(mp2.isPlaying()) {
   mp2.pause();
} else {
   mp2.start();
}

Upvotes: 1

Howli
Howli

Reputation: 12469

You could have a boolean check to see if it is started (and set the boolean to false) and if so stop the music, if not start it (and set the boolean to true). Something like:

public void onClick(View v) 
{
    if(musicPlaying == false)
    {
        mp2.start();
        musicPlaying = true;
    }
    else
    {
        mp2.stop();
        musicPlaying = false;
    }
}

Upvotes: 2

Related Questions