android - MediaPlayer doesn't resume video after activity restart -
i have mediaplayer
object uses surfaceholder
object surface. there button on top of video takes me out of video website. when happens, pause player player.pause()
. when return website, resume player player.start()
. know surface gets destroyed when activity not displayed anymore, , gets recreated activity restarted. in surfacecreated()
, set surface player again (since no longer has surface @ point), , resume. however, player restarts video beginning.
i've tried commenting out line takes me website, see if pause/start works , resumes last spot. does. i'm not sure why behaviour doesn't happen when leave , re-enter video activity though.
i tried using player.seekto()
call. there no difference. in fact, when disabled button taking me site pausing video, seekto()
call video started beginning despite position being not 0.
the player object same way throughout.
just because surface new 1 on restart, doesn't know or care of contents, it? player should managing that, right?
i'm out of ideas @ point. can please offer tips?
update: threw quick app eliminate other external factors. here's full code video class (other class activity play button):
public class videoplayer extends activity implements mediaplayer.oncompletionlistener, mediaplayer.onerrorlistener, mediaplayer.onpreparedlistener, mediaplayer.onseekcompletelistener, mediaplayer.onvideosizechangedlistener, surfaceholder.callback { private mediaplayer player; private surfaceholder msurfaceholder; private surfaceview msurfaceview; private button leavevideobutton; private boolean ispaused = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.video_layout); leavevideobutton = (button) findviewbyid(r.id.go_to_web); leavevideobutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { uri uri = uri.parse("http://www.google.com"); intent intent = new intent(intent.action_view, uri); pausesteps(); startactivity(intent); } }); createplayer(); createsurface(); } private void createsurface() { msurfaceview = (surfaceview) findviewbyid(r.id.surface); msurfaceholder = msurfaceview.getholder(); msurfaceholder.addcallback(this); msurfaceholder.settype(surfaceholder.surface_type_push_buffers); } private void createplayer() { player = new mediaplayer(); player.setoncompletionlistener(this); player.setonerrorlistener(this); player.setonpreparedlistener(this); player.setonvideosizechangedlistener(this); player.setonseekcompletelistener(this); } private void pausesteps() { if(player.isplaying()) { player.pause(); ispaused = true; } } private void playsteps() { if(ispaused) { ispaused = false; player.start(); } } @override protected void ondestroy() { super.ondestroy(); if (player.isplaying()) { player.stop(); } player.reset(); player.release(); player = null; } @override public void surfacecreated(surfaceholder holder) { player.setdisplay(holder); if (!ispaused) { try { // player.setdatasource(path); assetfiledescriptor afd = getresources().openrawresourcefd(r.raw.video); if (afd == null) return; player.setdatasource(afd.getfiledescriptor(), afd.getstartoffset(), afd.getlength()); afd.close(); player.prepare(); } catch (ioexception e) { e.printstacktrace(); } } else { playsteps(); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { } @override public void oncompletion(mediaplayer mp) { } @override public boolean onerror(mediaplayer mp, int what, int extra) { return false; } @override public void onprepared(mediaplayer mp) { player.start(); } @override public void onseekcomplete(mediaplayer mp) { } @override public void onvideosizechanged(mediaplayer mp, int width, int height) { } }
update 2: tried different video , resumed fine same spot. must encoding issue.
Comments
Post a Comment