Skip to content
Advertisement

Java – Android Media Player – change icon play to pause. Back not working

i have a code for playing sound from firebase storage. I setting now for ImageView change icon play to pause for playing music. I need the icon to go back to play after playing. Does anyone know how to do this?

public void song444 (View v)
{
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource("https://test.test.mp3");
        mediaPlayer.setOnPreparedListener(MediaPlayer::start);
        mediaPlayer.prepare();
        if(!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            playiIcon.setImageResource(R.drawable.ic_pause);}
    }catch (IOException e)
    {
        e.printStackTrace();
    }
}

Thank you for your help.

Advertisement

Answer

Just add a setOnCompletionListener:

      mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                playiIcon.setImageResource(R.drawable.ic_play);
            }
        });
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement