5

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();
}
Jonas
130k103 gold badges330 silver badges408 bronze badges
asked Aug 8, 2013 at 7:22
2
  • Create a boolean value for that, if it is "PLAY" then call the "pause()", toggle the variable and vice versa. Commented Aug 8, 2013 at 7:28
  • Can you help me edit my code? I'm a newbie to android. Commented Aug 8, 2013 at 7:30

4 Answers 4

10

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;
}
});
answered Aug 8, 2013 at 7:29
Sign up to request clarification or add additional context in comments.

4 Comments

it says change modifier of buttonStatus into final
then when i put final boolean, it says it cannot be defined since its in enclosing type.
declare it as a class variable
you have to put the variable isPlaying outside of your function. for example direct under your class name
0
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
 }
 }
}
answered Aug 8, 2013 at 7:33

1 Comment

it says change modifier of b into final, then when i put final boolean, it says it cannot be defined since its in enclosing type.
0

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

answered Aug 8, 2013 at 7:34

Comments

0

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");
 }
 }
answered Aug 10, 2013 at 5:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.