How to play and pause in only one button - Android

2019-03-21 19:49发布

I have two buttons in my media player that streams a radio station, play and pause. I want to make it only one button that has two function. First click I want to play it and second click I want to pause it. And when I click it again I want to play it. I need help. Here is my code.

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


 play.setOnClickListener(new View.OnClickListener() {
 public void onClick(View view) {
 play();
}
});
play.performClick();

pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
pause();
}
});


}

private void play() {
Uri myUri = Uri.parse("Shoutcast URL");
try {
if (mp == null) {
this.mp = new MediaPlayer();
} else {
mp.stop();
mp.reset();
}
mp.setDataSource(this, myUri); // Go to Initialized state
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnBufferingUpdateListener(this);

mp.setOnErrorListener(this);
mp.prepareAsync();

Log.d(TAG, "LoadClip Done");
} catch (Throwable t) {
Log.d(TAG, t.toString());
}
}

@Override
public void onPrepared(MediaPlayer mp) {
Log.d(TAG, "Stream is prepared");
mp.start();
}

private void pause() {
mp.pause();
}



@Override
public void onDestroy() {
super.onDestroy();
stop();

}

public void onCompletion(MediaPlayer mp) {
stop();
}

5条回答
混吃等死
2楼-- · 2019-03-21 20:29

You can use Toggle button:

toggleButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    if (toggleButton.isChecked()) {
       play();
    } else {
        pause();
    }
}
});

You can also remove the green bar from toggle button.

Toggle button Mkyong example

Android dev tutorial

查看更多
孤傲高冷的网名
3楼-- · 2019-03-21 20:35

Create just one Button and add something like a buttonstatus. Then you can check the status in your listener.
For example:

boolean isPlaying = false;
playPause = (Button) findViewById(R.id.play);


 playPause.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
   if (isPlaying) {
     pause();
   }else{
     play();
   }
   isPlaying = !isPlaying;
}
});
查看更多
可以哭但决不认输i
4楼-- · 2019-03-21 20:43

For example, here's a ToggleButton with the android:onClick attribute:

<ToggleButton 
    android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"
    android:onClick="onToggleClicked"/>

Within the Activity that hosts this layout, the following method handles the click event:

public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        // Enable vibrate
    } else {
        // Disable vibrate
    }
}
查看更多
Root(大扎)
5楼-- · 2019-03-21 20:48

You don't need to declare any boolean variable to check player state. There is a method already available in mediaplayer class named - isPlaying().

Try my code sequence for playpause using single button.

public void playpause(){

            if(!mp.isPlaying()){ // here mp is object of MediaPlayer class
            mp.start();
                //showNotification("Player State Plying");
            }
        else if(mp.isPlaying()){
                mp.pause();
                //showNotification("Player State Pause");

            }


    }
查看更多
6楼-- · 2019-03-21 20:49
Boolean b = false;
Button play = (Button) findViewById(R.id.play);
play..setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
    if(b == false)
  {
         //write play code here
       b= true;
  }
  else
  {
     // enter pause code here
   }

   }
}
查看更多
登录 后发表回答