Reputation: 11
Here I have 2 buttons for the audio controller, play and pause. How can I combine both buttons into one? I also need the image icon to change as well.
In onCreate, I have initially disabled Pause button.
pauseButton.setEnabled(false);
Below is the implementation for Play button.
public void play(View view){
mediaPlayer.start();
...
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
Below is the implementation for Pause button.
public void pause(View view){
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
Upvotes: 1
Views: 1164
Reputation: 1123
You can do this by 2 ways :
1. Take a frame layout and put two buttons overlapping each other (by default set visible to play button and hide the pause button) and set visibility according to it like
public void play(View view) {
if(pauseButton.isVisible()) {
pauseButton.setVisibility(View.GONE);
}
playButton.setVisibility(View.VISIBLE);
}
public void pause(View view) {
if(playButton.isVisible()) {
playButton.setVisibility(View.GONE);
}
pauseButton.setVisibility(View.VISIBLE);
}
2. You can take only one button and change it backgroundResource (by default set background resource play) like
public void play(View view) {
Button.setBackgroundResourec(R.drawable.pause);
}
public void pause(View view) {
Button.setBackgroundResourec(R.drawable.play);
}
Upvotes: 1
Reputation: 864
I know it is quite late to answer this question, but i have searched for this question and google shown me this at the top of result, but as i see there is not satisfactory result is available here, So here i am posting my code so that it could help other
fabBtnPlayAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!playPause) {
fabBtnPlayAudio.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
if (intialStage){
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource("URL for audio file.. ");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
fabBtnPlayAudio.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
if (mediaPlayer.isPlaying()){
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
playPause = false;
}
}
});
Upvotes: 0
Reputation: 459
Make a single button and take a flag as
Boolean buttonflag=true;//initially true
Suppose true means play and false will mean pause.
public void button(View view)
{
if(buttonflag)//when true play
{
mediaPlayer.play();
buttonflag=false;//set to false so that on next click else will work
}
else//when false pause
{
mediaPlayer.pause();
buttonflag=true;//set to true so that on next click if will work
}
}
Upvotes: 1
Reputation: 707
MediaPlayer m1 = null;
playAndStopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopPlaying();
m1=MediaPlayer.create(MainActivity.this, R.raw.sound1);
m1.start();
}
});
private void stopPlaying() {
if (mp1 != null) {
m1.stop();
m1.release();
m1 = null;
}
This should work for you! Good luck.
Upvotes: 0