Reputation: 35
I am making an app with Android Studio and I have an mp3 file in my app. I have added a play and pause button in it but my problem is that when I click on the play button, the pause button shows, but when an mp3 file finishes playing there is still a pause button display. I want it to change to the play button again automatically when the audio is finished. This is my code:
play.setImageResource(R.drawable.play);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null && mp.isPlaying()) {
mp.pause();
play.setImageResource(R.drawable.play);
length = mp.getCurrentPosition();
} else {
mp.seekTo((int) length);
mp.start();
play.setImageResource(R.drawable.pause);
Upvotes: 0
Views: 636
Reputation: 59
instead of using one button try creating two - Play and Pause...(I assume you are already using Relativelayout
) and then use pause.setVisibility(View.GONE)
& play.setVisibility(View.VISIBLE)
Upvotes: 0
Reputation: 100
you can use setOnCompletionListener;
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Do something when media player end playing
play.setImageResource(R.drawable.play);
}
});
Upvotes: 4