Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android song control

Tags:

android

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();
        }   

    }

}
like image 535
Prabu JM Avatar asked Sep 02 '26 08:09

Prabu JM


2 Answers

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

if(mp.isPlaying())
{
    mp.stop();
    //rest of your code
}
like image 93
PaperThick Avatar answered Sep 03 '26 21:09

PaperThick


public void pl(int songindex)
{
if(mp!=null)
{
 mp.stop()
 ........
 ........\\your code here
 }
 else if(mp==null)
 {
  ...... 
  ......\\your code here
 }
}
like image 23
RJHP Avatar answered Sep 03 '26 22:09

RJHP