How to resume MediaPlayer in Android after pressing the home button and reopen the app -
i tried lot of methods, made lot of changes on code, read android mediaplayer document, tried stackoverflow examples none of them solve problem.
my problem: when press home button of emulator or phone reopen app starting beginning.
hope can me. in advance.
here code :
public class mediaplayer extends activity implements oncompletionlistener, onerrorlistener, oninfolistener, onpreparedlistener, onseekcompletelistener, onvideosizechangedlistener, surfaceholder.callback, mediacontroller.mediaplayercontrol { display currentdisplay; surfaceview surfaceview; surfaceholder surfaceholder; mediaplayer mediaplayer; mediacontroller controller; int videowidth = 0; int videoheight = 0; boolean readytoplay = false; public final static string logtag = "custom_video_player"; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.video); surfaceview = (surfaceview) this.findviewbyid(r.id.surfaceview); surfaceholder = surfaceview.getholder(); surfaceholder.addcallback(this); surfaceholder.settype(surfaceholder.surface_type_push_buffers); mediaplayer = new mediaplayer(); mediaplayer.setoncompletionlistener(this); mediaplayer.setonerrorlistener(this); mediaplayer.setoninfolistener(this); mediaplayer.setonpreparedlistener(this); mediaplayer.setonseekcompletelistener(this); mediaplayer.setonvideosizechangedlistener(this); mediaplayer.setwakemode(getapplicationcontext(), powermanager.partial_wake_lock); string filepath = "http://cdn.example.com/wp-content/uploads/almost.human-s01e10.mp4"; try { mediaplayer.setdatasource(filepath); } catch (illegalargumentexception e) { log.v(logtag, e.getmessage()); finish(); } catch (illegalstateexception e) { log.v(logtag, e.getmessage()); finish(); } catch (ioexception e) { log.v(logtag, e.getmessage()); finish(); } controller = new mediacontroller(this); currentdisplay = getwindowmanager().getdefaultdisplay(); } public void surfacecreated(surfaceholder holder) { // log.v(logtag, "surfacecreated called"); mediaplayer.setdisplay(holder); try { mediaplayer.prepare(); } catch (illegalstateexception e) { // log.v(logtag, e.getmessage()); finish(); } catch (ioexception e) { // log.v(logtag, e.getmessage()); finish(); } } public void surfacechanged(surfaceholder holder, int format, int width, int height) { log.v(logtag, "surfacechanged called"); } public void surfacedestroyed(surfaceholder holder) { log.v(logtag, "surfacedestroyed called"); } public void oncompletion(mediaplayer mp) { log.v(logtag, "oncompletion called"); finish(); } public boolean onerror(mediaplayer mp, int whaterror, int extra) { log.v(logtag, "onerror called"); if (whaterror == mediaplayer.media_error_server_died) { log.v(logtag, "media error, server died " + extra); } else if (whaterror == mediaplayer.media_error_unknown) { log.v(logtag, "media error, error unknown " + extra); } return false; } public boolean oninfo(mediaplayer mp, int whatinfo, int extra) { if (whatinfo == mediaplayer.media_info_bad_interleaving) { log.v(logtag, "media info, media info bad interleaving " + extra); } else if (whatinfo == mediaplayer.media_info_not_seekable) { log.v(logtag, "media info, media info not seekable " + extra); } else if (whatinfo == mediaplayer.media_info_unknown) { log.v(logtag, "media info, media info unknown " + extra); } else if (whatinfo == mediaplayer.media_info_video_track_lagging) { log.v(logtag, "mediainfo, media info video track lagging " + extra); /* * android version 2.0 , higher } else if (whatinfo == * mediaplayer.media_info_metadata_update) { * log.v(logtag,"mediainfo, media info metadata update " + extra); */ } return false; } public void onprepared(mediaplayer mp) { log.v(logtag, "onprepared called"); videowidth = mp.getvideowidth(); videoheight = mp.getvideoheight(); if (videowidth > currentdisplay.getwidth() || videoheight > currentdisplay.getheight()) { float heightratio = (float) videoheight / (float) currentdisplay.getheight(); float widthratio = (float) videowidth / (float) currentdisplay.getwidth(); if (heightratio > 1 || widthratio > 1) { if (heightratio > widthratio) { videoheight = (int) math.ceil((float) videoheight / (float) heightratio); videowidth = (int) math.ceil((float) videowidth / (float) heightratio); } else { videoheight = (int) math.ceil((float) videoheight / (float) widthratio); videowidth = (int) math.ceil((float) videowidth / (float) widthratio); } } } surfaceview.setlayoutparams(new linearlayout.layoutparams(videowidth, videoheight)); mp.start(); controller.setmediaplayer(this); controller.setanchorview(this.findviewbyid(r.id.mainview)); controller.setenabled(true); controller.show(); } @override protected void onpause() { super.onpause(); if (mediaplayer.isplaying()) { mediaplayer.pause(); } else { return; } } public void onseekcomplete(mediaplayer mp) { log.v(logtag, "onseekcomplete called"); } public void onvideosizechanged(mediaplayer mp, int width, int height) { log.v(logtag, "onvideosizechanged called"); } public boolean canpause() { return true; } public boolean canseekbackward() { return true; } public boolean canseekforward() { return true; } public int getbufferpercentage() { return 0; } public int getcurrentposition() { return mediaplayer.getcurrentposition(); } public int getduration() { return mediaplayer.getduration(); } public boolean isplaying() { return mediaplayer.isplaying(); } public void pause() { mediaplayer.pause(); } public void seekto(int pos) { mediaplayer.seekto(pos); } public void start() { mediaplayer.start(); } @override public boolean ontouchevent(motionevent ev) { controller.show(); return false; } @override public int getaudiosessionid() { // todo auto-generated method stub return 0; } }
i understand have accepted answer i'll explain problem based on comments on question , comments , nana on nana's answer.
- a
surfaceview
destroyed when no longer visible , recreated when becomes visible again. - you calling
mp.pause()
inactivity
onpause()
method. - when go 'recent' apps list , attempt restart app,
surfaceview
recreated. - in
surfacecreated
method callingmp.prepare()
@ pointmp
in paused state , callingprepare()
on pausedmediaplayer
throwillegalstateexception
. - you have
try / catch
block catchesillegalstateexception
, callsfinish()
- why first attempt restart app 'recent' list causes theactivity
destroyed. - as result of above sequence of events need second attempt re-open app 'recent' list
activity
has been destroyed go through full creation (withoncreate(..)
being called. why starts beginning.
the answer nana workaround still mean having use 2 attempts restart activity
'recent' list.
unfortunately mediaplayer
class lacking in methods checking 'state' isplaying
being useful method state. it's shame class devs didn't think add getstate()
method (or similar) allow checking whether it's started, playing, stopped, paused etc.
one way of preventing illegalstateexception
have boolean called ispaused
(for example) modify activity
onpause()
follows...
if (mediaplayer.isplaying()) { mediaplayer.pause(); ispaused = true; }
...and in surfacecreated(...)
...
try { if (ispaused) { mpstart(); ispaused = false; } else mediaplayer.prepare(); } // catch blocks here
Comments
Post a Comment