BossWalrus
BossWalrus

Reputation: 770

Switch Play and Pause Image/Audio Android

I'm having trouble with the play and pause buttons for my app. I'm trying to make it so that when I click "Play" the music plays and the pause button is showing, and when I click the button again, the music is paused and the play button is showing again. I feel like I am so very close, but just can't get it! I'm able to get the audio to play and the button to change images once, but then I'm unable to pause it and have the images switch back. Please help! My code is below:

button3.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(mysong.isPlaying()){
            mysong.pause();
            button3.setVisibility(ImageButton.VISIBLE);
            pausebutton3.setVisibility(ImageButton.GONE);


        }else {
            mysong.start();
                            button3.setVisibility(ImageButton.GONE)
                            pausebutton3.setVisibility(ImageButton.VISIBLE)

                 }
    }
});

When I have it set like that now, the music will play and the play button will be swapped out with the pause button (as it should), but now I can't get the music to pause and the image to revert back.

Upvotes: 1

Views: 4724

Answers (2)

Tim Malseed
Tim Malseed

Reputation: 6373

The reason yours doesn't 'switch back' is because there is nothing in the 'else' statement to tell it to do any UI changes.

If it is playing and the button is pressed, button3 becomes VISIBLE and mysong is GONE, and isPlaying() becomes false. But if the button is pressed again, because isPlaying() is false, it goes straight into the else, and calls mysong.start(); - There is nothing to tell the visibility of mysong or button3 to change back.

Alternatively, try using ImageButton and setImageResource() if you want to just change the image (rather than hiding and showing the button);

    mPauseButton.setOnClickListener(new View.OnClickListener() { 

        public void onClick(View v) { 
            if (mysong.isPlaying()) {
                mysong.pause();
                button3.setImageResource(R.drawable.btn_play);  //Your playbutton image
            } else {
                mysong.start();
                button3.setImageResource(R.drawable.btn_pause);  //Your pausebutton image
            }
            .
            .
            .

The other thing I've noticed is you are trying to set the visibility of mySong as well as button3 (mysong seems to be your mediaPlayer - it doesn't have 'visibility' and isn't an ImageButton).

Upvotes: 3

Royston Pinto
Royston Pinto

Reputation: 6731

If i understand, you have one button, which toggles between play/pause.

button3.setOnClickListener(new View.OnClickListener() { 

public void onClick(View v) { 
        if(mysong.isPlaying()){ 
        mysong.pause(); 
        button3.setVisibility(ImageButton.VISIBLE);
        button3.setText("Pause");
        mysong.setVisibility(ImageButton.GONE); 
    } else { 
        mysong.start();
        button3.setText("Play");
    } 
} 

});

Upvotes: 1

Related Questions