Prabu JM
Prabu JM

Reputation: 31

Android song control

How to stop previous playing song before playing next song and i get the after alter my code,this is my altered code

public void pl(int songindex)
{
    if(mp.isPlaying())
    {
        mp.stop();
        if(songindex==0)
        {

            mp=MediaPlayer.create(this, R.raw.gayatri);
            mp.start();
        }

        else if(songindex==1)
        {

            mp=MediaPlayer.create(this, R.raw.brahma);
            mp.start();
        }   

    }

}

Upvotes: 3

Views: 123

Answers (3)

PaperThick
PaperThick

Reputation: 2799

When you click the "next song" button run this code first

if(mp.isPlaying())
{
    mp.stop();
    //rest of your code
}

Upvotes: 4

RJHP
RJHP

Reputation: 87

public void pl(int songindex)
{
if(mp!=null)
{
 mp.stop()
 ........
 ........\\your code here
 }
 else if(mp==null)
 {
  ...... 
  ......\\your code here
 }
}

Upvotes: 3

Pragnani
Pragnani

Reputation: 20155

Try this

if(mp.isPlaying())
{
mp.stop();
}

Upvotes: 2

Related Questions