Skip to content
Advertisement

MediaPlayer to stop current song when new song is selected in ListView

I am writing a music player app using ListView. When i click a song on the MainActivity, it opens a nowplaying activity and a song starts to play, but if i go back to the ListView and click a new song, the new songs starts to play while the previous song is still playing so both songs play at the same time.

How do i make it such that when a new song is clicked, the previous song stops and the new song begins? Below is my code for the MainActivity

public class nowPlaying extends AppCompatActivity implements View.OnClickListener {

Button rewindButton, pauseAndPlayButton, fastForwardButton;
TextView songNameID, artistNameID, details, detailsText;
ImageView musicLogo;
SeekBar seekBar;
public static MediaPlayer mediaPlayer;
public Runnable runnable;
public Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_now_playing);

    TextView nowPlayingID = findViewById(R.id.now_playing_id);
    songNameID = findViewById(R.id.song_name_id);
    artistNameID = findViewById(R.id.artist_name_id);
    rewindButton = findViewById(R.id.rewind_button);
    pauseAndPlayButton = findViewById(R.id.pause_and_play_button);
    fastForwardButton = findViewById(R.id.fast_forward_button);
    detailsText = findViewById(R.id.details_text);
    details = findViewById(R.id.details_id);
    seekBar = findViewById(R.id.seekbar_id);
    musicLogo = findViewById(R.id.music_logo);
    handler = new Handler();

    nowPlayingID.setText(R.string.now_playing);
    rewindButton.setText("<<");
    pauseAndPlayButton.setText(R.string.ii);
    fastForwardButton.setText(">>");
    details.setText(R.string.about_song);

    String nameSong = getIntent().getStringExtra("name");
    String nameArtist = getIntent().getStringExtra("artist");

    if (nameSong.equals("Shape of you") && nameArtist.equals("ed sheeran")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_ed_sheeran);
        musicLogo.setImageResource(R.drawable.shape_of_you);

        mediaPlayer = MediaPlayer.create(this, R.raw.sheeran);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }

    else if (nameSong.equals("Friends") && nameArtist.equals("marshmello & anne-marie")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_marshmello);
        musicLogo.setImageResource(R.drawable.marshmello);

        mediaPlayer = MediaPlayer.create(this, R.raw.marshmello);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }

    else if (nameSong.equals("L'Hiver Indien") && nameArtist.equals("baloji")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_baloji);
        musicLogo.setImageResource(R.drawable.baloji);

        mediaPlayer = MediaPlayer.create(this, R.raw.baloji);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }

    else if (nameSong.equals("Ye") && nameArtist.equals("burna boy")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_burna);
        musicLogo.setImageResource(R.drawable.burna);

        mediaPlayer = MediaPlayer.create(this, R.raw.burna);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }

    else if (nameSong.equals("What lovers do") && nameArtist.equals("maroon 5 ft. sza")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_maroon);
        musicLogo.setImageResource(R.drawable.maroon_five);

        mediaPlayer = MediaPlayer.create(this, R.raw.maroon);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }

    else if (nameSong.equals("Live in the moment") && nameArtist.equals("portugal. the man")) {
        songNameID.setText(nameSong);
        artistNameID.setText(nameArtist);
        detailsText.setText(R.string.details_portugal);
        musicLogo.setImageResource(R.drawable.portugal_man);

        mediaPlayer = MediaPlayer.create(this, R.raw.portugal);

        rewindButton.setOnClickListener(this);
        pauseAndPlayButton.setOnClickListener(this);
        fastForwardButton.setOnClickListener(this);


        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                else {
                    mediaPlayer.start();
                    changeSeekbar();
                }
            }
        });
    }





    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            if (b) {
                mediaPlayer.seekTo(i);
            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

public void changeSeekbar() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());
    if (mediaPlayer.isPlaying()) {
        runnable = new Runnable() {
            @Override
            public void run() {
                changeSeekbar();
            }
        };
        handler.postDelayed(runnable, 1000);
    }
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.pause_and_play_button:
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                pauseAndPlayButton.setText(">");
            }
            else {
                mediaPlayer.start();
                pauseAndPlayButton.setText("||");
                changeSeekbar();
            }
            break;

        case R.id.fast_forward_button:
            mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() + 5000);
            break;

        case R.id.rewind_button:
            mediaPlayer.seekTo(mediaPlayer.getCurrentPosition() - 5000);
            break;
    }
}
}    

Advertisement

Answer

When switching to a new audio file, the mediaPlayer pointer/variable needs to be decalred null first before assigning a new audio source

public void stop() {
    if (mp != null) {
        mp.release();
        mp = null;
    }
}

public void play(int rid) {
    stop();

    mp = MediaPlayer.create(this, rid);
    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            stop();
        }
    });

    mp.start();
}
Advertisement