mehmetk
mehmetk

Reputation: 19

Android - Second Press Play Music in Listview

I have a listview. When i pressed my first music, it is playing. When i pressed same music again, i have an error. I want that i can play and pause as i want. First press is play, second press is stop, thirth press is again play

Here is code

public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)
       {
        switch(position)
        {
        case 0 :
            if(sound.isPlaying())
            {
                sound.stop();
                sound.release();
            }
            if(!sound.isPlaying())
            {
                sound.start();
            }
         break;

Upvotes: 1

Views: 174

Answers (1)

steevoo
steevoo

Reputation: 631

try to initiate player before play

public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id)

    {
        switch(position)
        {
        case 0 :
            if(sound.isPlaying())
            {
                sound.stop();
                sound.release();
            }
            if(!sound.isPlaying())

            {
                sound = new MediaPlayer();
                sound.setDataSource(filename);
                sound.prepare(); 
                sound.start();
            }

Upvotes: 1

Related Questions