Search code examples
androidandroid-activitymp3android-togglebutton

play and stop or pause mp3


ok my toggle button is working fine , but my mp3 not got started after when its stopped. here is my code please do tell me proper way to stop and start again mp3 sound. here is my code and debug logs.

public void toggleclick(View v) throws IllegalStateException, IOException{
     if(tg.isChecked())
     {
         if( mp_tick.isPlaying())
             mp_tick.stop();
             //mp_tick.release();
        Toast.makeText(this, "ON", Toast.LENGTH_SHORT).show();
     }
     else

         mp_tick.prepare();
         mp_tick.start();
    // mp_tick.start();
        Toast.makeText(this, "OFF", Toast.LENGTH_SHORT).show();
     }

here is my log:

02-07 16:21:43.651: D/MediaPlayer(24554): start() in
02-07 16:21:43.651: E/MediaPlayer(24554): start called in state 0
02-07 16:21:43.651: E/MediaPlayer(24554): error (-38, 0)
02-07 16:21:43.651: D/MediaPlayer(24554): start() out
02-07 16:21:44.652: D/MediaPlayer(24554): start() in
02-07 16:21:44.652: E/MediaPlayer(24554): start called in state 0
02-07 16:21:44.652: E/MediaPlayer(24554): error (-38, 0)
02-07 16:21:44.652: D/MediaPlayer(24554): start() out

Solution

  • After you called mediaPlayer.stop() you can try at this at your else-case in your togglelistener:

    mp.reset();
    mp.setDataSource(MEDIA_PATH);
    mp.prepare();
    mp.start();
    

    You also can try this:

    instead of mediaPlayer.stop():

    mp.pause();
    mp.seekTo(0);
    

    and start just like:

     mp.start();
    

    Good luck!