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

别等时光非礼了梦想. 提交于 2019-11-30 10:39:00

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.

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;
    }
}
if(mp2.isPlaying()) {
   mp2.pause();
} else {
   mp2.start();
}

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();
            }
        }   
    });

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. :-)

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!