i have created demo media player in android.i'm facing problem while start run application.when app run song playing not playing full song start , finish means start activity , goes resume() state.and when song on again restart activity song not playing beginning start middle.i'm facing problem last 1 week , don't understand how solve .please can 1 me.here code.thanks in advanced.
public class audio_activity extends activity { private mediaplayer mp = null; phonestatelistener phlistener; int length; sharedpreferences prefs; imageview imgvw; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.audio); init(); imgvw.setimageresource(r.raw.teddy_two); prefs = preferencemanager.getdefaultsharedpreferences(this); final sharedpreferences. editor prefsedit = prefs.edit(); mp=mediaplayer.create(audio_activity.this,r.raw.issaq_tera_by_vishu); log.e("song playing","in mediya player "); mp.start(); mp.setlooping(false); system.out.println("media plyer start !!!"); prefsedit.putboolean("mediaplaying", true); prefsedit.commit(); btnchapter.setenabled(false); mp.setoncompletionlistener(new mediaplayer.oncompletionlistener() { @override public void oncompletion(mediaplayer mp) { // todo auto-generated method stub mp.stop(); system.out.println("media plyer complete !!!"); //mp.release(); prefsedit.putboolean("mediaplaying", false); prefsedit.commit(); btnchapter.setenabled(true); system.out.println("music on , button enable !!!!!!"); } }); phonestatelistener phonestatelistener = new phonestatelistener() { @override public void oncallstatechanged(int state, string incomingnumber) { if (state == telephonymanager.call_state_ringing) { if(mp!=null) { setplayerbutton(true, false, true); if(mp.isplaying()) { mp.pause(); } } } else if(state == telephonymanager.call_state_idle) { } else if(state == telephonymanager.call_state_offhook) { } super.oncallstatechanged(state, incomingnumber); } }; telephonymanager mgr = (telephonymanager) getsystemservice(telephony_service); if(mgr != null) { mgr.listen(phonestatelistener, phonestatelistener.listen_call_state); } } private void setplayerbutton(boolean play, boolean pause, boolean stop){ btnstartstop.setenabled(play); if(play==true) btnstartstop.setenabled(true); else btnstartstop.setenabled(false); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); setrequestedorientation(activityinfo.screen_orientation_landscape); } @override public void onpause() { super.onpause(); sharedpreferences. editor prefsedit = prefs.edit(); boolean isplaying=prefs.getboolean("mediaplaying",false); if(isplaying) { int position = mp.getcurrentposition(); log.e("current ","position -> " + position); prefsedit.putint("mediaposition", position); prefsedit.commit(); } } @override protected void onresume() { super.onresume(); system.out.println("activity resume !!!"); boolean isplaying=prefs.getboolean("mediaplaying",false); if(isplaying) { int position = prefs.getint("mediaposition", 0); mp.seekto(position); mp.start(); } } @override public boolean onkeydown(int keycode, keyevent event) { super.onstop(); if ((keycode == keyevent.keycode_back)) { if(mp!= null) { if(mp.isplaying()) { mp.pause(); system.out.println("media player pause/stop click on button on emulator!!!"); } } finish(); return true; } return super.onkeydown(keycode, event); } }
the starting middle of song problem because store current progress of song in sharedpreferences
, @ onresume()
start player progressed position stored @ sharedpreferences
Comments
Post a Comment