Android Audio Player Start playing another song with already playing song

I m Making an audio player (android).The question is when i start my app > play Song >press the back key or home the song Still play's in background But when i open the app again it start new song playing where the old song is also playing. How to get Rid of this?

Upvotes: 0

Views: 2370

Answers (3)

Arun
Arun

Reputation: 173

Use mediaplayer.reset(); after stop() method before playing new song.

Add these points in select & play new song method:

  1. Check if the track is playing:

    if(mp.isPlaying())
    

    ... where mp is MediaPlayer object.

  2. If yes then stop play and then reset media player:

    mp.stop();  
    mp.reset();
    

    The mp.reset() will reset all initialization of MediaPlayer ex: song URI etc.

    If not then only reset media player:

    mp.reset();  
    
  3. Now get the position and the ID of newly selected track from the list and set it as new URI.

  4. Play track based on new URI:

    mp = MediaPlayer.create(this, NewTrackUri);  
    mp.start();
    

    This should only play the newly selected track.

Upvotes: 1

The Problem Was i was not aware of Service which run in background. Now i am using a service for plating song and updating activity UI from service and when my app is in background the song plays and as i reopen the app and select another song the previous stop playing and new song start playing Thanks every body for help

Upvotes: 0

Blaze Tama
Blaze Tama

Reputation: 10948

Stop and release the media player in the onDestroy, example :

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(mp!=null){
         mp.stop();
         mp.release();
         mp = null;
    }
}

Upvotes: 2

Related Questions