android - Media Player Crashing -
i new programming , upon referring google's dev site came simple media player plays file selected user. app seems been running fine when choosing file played first time crashes right after selecting file second time. have pasted code below. appreciated.
public class mainactivity extends activity { private static int reqs =1; private string a; mediaplayer md=new mediaplayer(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button start = (button) findviewbyid(r.id.button); final button stop = (button) findviewbyid(r.id.button3); final button pause = (button) findviewbyid(r.id.button2); final button select = (button) findviewbyid(r.id.button4); pause.setenabled(false); stop.setenabled(false); start.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { md.start(); toast.maketext(getapplicationcontext(), "playing", toast.length_short).show(); pause.setenabled(true); stop.setenabled(true); } }); stop.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { pause.setenabled(false); toast.maketext(getapplicationcontext(), "stopped", toast.length_short).show(); md.stop(); } }); pause.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { md.pause(); toast.maketext(getapplicationcontext(), "paused", toast.length_short).show(); } }); select.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent intent = new intent(intent.action_get_content); intent.settype("audio/mpeg"); startactivityforresult(intent.createchooser(intent, "choose"), reqs); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode <= reqs && resultcode ==-1) { uri videouri = data.getdata(); = videouri.tostring(); md.setdatasource(a); //try-catch surrounding md.prepare(); //try-catch surrounding } } }
you not calling reset()
reset state of mediaplayer
object.
as android's documentation states:
in order reuse mediaplayer object in error state , recover error, reset() can called restore object idle state.
check at: https://developer.android.com/reference/android/media/mediaplayer.html
Comments
Post a Comment