How to handle Audio continously with Landscape and portrait in Android? -
i have created audio in 1 activity media player. have run start, pause, resume when rotate screen landscape or portrait mode song playing twice.
here code:
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.audio); init(); prefs = preferencemanager.getdefaultsharedpreferences(this); final sharedpreferences.editor prefsedit = prefs.edit(); mp = mediaplayer.create(audio_activity.this, r.raw.subhanallah); log.e("song playing", "in mediya player "); mp.setlooping(false); mp.start(); system.out.println("media plyer start !!!"); prefsedit.putboolean("mediaplaying", true); prefsedit.commit(); btnchapter.setenabled(false); system.out.println("b4 button click!!!!"); } @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) { mp.pause(); int position = mp.getcurrentposition(); log.e("current ", "position -> " + position); prefsedit.putint("mediaposition", position); prefsedit.commit(); } } @override protected void onresume() { super.onresume(); mp.start(); 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) { if ((keycode == keyevent.keycode_back)) { if (mp != null) { if (mp.isplaying()) { mp.stop(); } } finish(); return true; } return super.onkeydown(keycode, event); } }
do not rely on sharedpreferences
value in onpause()
. use start player in oncreate()
.
Comments
Post a Comment