java - How to make a stop button function with a play/pause toggle and seekBar widget -
i have been trying can stop song in android studio app mediaplayer package. have place song inside app , plays fine when play , pause too. however, when play song or pause song after pressed stop function, unexpectedly crash app saying "unfortunately, playpausestop has stopped." can sure going on in stop function, not quite sure need stop actual song , restart beginning. additionally, trying add seekbar reads length of song duration. sure need setduration();, setmax();, , more. appreciate helpful resource can use track trying this. here code far, not best:
package com.example.hamzeh.playpausestop; import android.media.mediaplayer; import android.media.mediaplayer; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.toast; import android.widget.togglebutton; import android.widget.seekbar; public class mainactivity extends appcompatactivity { seekbar seek_bar; int pause; protected static mediaplayer sound; // @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); sound = mediaplayer.create(mainactivity.this, r.raw.music); } public void ontoggleclicked(view view) { boolean checked = ((togglebutton)view).ischecked(); if (checked && !sound.isplaying() && sound!=null) { sound.start(); //play song } else if (sound.isplaying()) { sound.pause(); pause = sound.getcurrentposition(); //pause on current position } else { toast.maketext(mainactivity.this, "something wrong", toast.length_short).show(); //shows message if music not existing in raw file } } public void stop(view view) { sound.release(); sound = null; }
}
(i trying learn how use android studio also)
why making sound = null , specific reason?? on stop can call rest instead
Comments
Post a Comment