Resume Android youtubePlayer in OnResume -
i have youtubebaseactivity works well, when click go activity , return activity, not auto play.
i resume , start playing lists again.
here class below :
public class videoplayer extends youtubebaseactivity implements youtubeplayer.oninitializedlistener,view.onclicklistener{ private youtubeplayerview ytp_mainvideo; private youtubeplayer vyoutubeplayer; private youtubeplayer.playerstatechangelistener mplayerstatechangelistener; boolean fullscreen =false; textview txtmainvideo; list<string> videolist; static string currentkey=""; utilities utilities = new utilities(this); @override public void onsaveinstancestate(bundle savedinstancestate) { // save state // call superclass can save view hierarchy state super.onsaveinstancestate(savedinstancestate); } @override protected void onresume() { super.onresume(); if (vyoutubeplayer == null) { ytp_mainvideo.initialize(config.developer_key, this); } } @override protected void onstop() { super.onstop(); if(vyoutubeplayer!=null) vyoutubeplayer.cuevideos(videolist); } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.vidsplash); currentkey = utilities.getlicensekey(); // initialize main video player. ytp_mainvideo =(youtubeplayerview)findviewbyid(r.id.youtubplayerview); ytp_mainvideo.initialize(config.developer_key, this); txtmainvideo =(textview)findviewbyid(r.id.text_mainvideo); txtmainvideo.setonclicklistener(this); resources res = getresources(); string[] varray = res.getstringarray(r.array.videolist); videolist = arrays.aslist(varray); mplayerstatechangelistener = new youtubeplayer.playerstatechangelistener() { @override public void onloading() { } @override public void onloaded(string s) { } @override public void onadstarted() { } @override public void onvideostarted() { } @override public void onvideoended() { log.d("showcase", "video ended"); if(vyoutubeplayer != null) vyoutubeplayer.loadvideos(videolist); } @override public void onerror(youtubeplayer.errorreason errorreason) { } }; if(currentkey.compareto("")==0) { intent = new intent(getapplicationcontext(), newstartactivity.class); startactivity(i); } } @override public void onclick(view v) { switch(v.getid()){ case r.id.text_mainvideo: intent intentsub=new intent(getapplicationcontext(),catemainactivity.class); startactivity(intentsub); break; } } @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer youtubeplayer, boolean wasrestored) { vyoutubeplayer =youtubeplayer; youtubeplayer.setplayerstatechangelistener(mplayerstatechangelistener); if (!wasrestored) { // loadvideo() auto play video // use cuevideo() method, if don't want play automatically youtubeplayer.loadvideos(videolist); // hiding player controls youtubeplayer.setplayerstyle(youtubeplayer.playerstyle.chromeless); youtubeplayer.setonfullscreenlistener(new youtubeplayer.onfullscreenlistener() { @override public void onfullscreen(boolean _isfullscreen) { fullscreen = _isfullscreen; } }); } } @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult youtubeinitializationresult) { } @override public boolean onkeydown(int keycode, keyevent event) { // todo auto-generated method stub if (keycode == keyevent.keycode_back) { // toast.maketext(appcontext, "back", toast.length_long).show(); alertdialog.builder alert = new alertdialog.builder( videoplayer.this); alert.settitle(getstring(r.string.app_name)); alert.seticon(r.drawable.app_icon); alert.setmessage("are sure want quit?"); alert.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int whichbutton) { //you may open interstitial ads here finish(); } }); alert.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub } }); alert.show(); return true; } return super.onkeydown(keycode, event); }
}
first declare youtubeplayer & seektime variable
private youtubeplayer youtubeplayer; private int seektime = 0;
then in oninitializationsuccess method set youtubeplayer
@override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer youtubeplayer, boolean wasrestored) { this.youtubeplayer = youtubeplayer; if (!wasrestored) { youtubeplayer.loadvideo(videoid); } }
after that, can store video current time in onpause , can resume video in onresume method using trick below
@override protected void onresume() { super.onresume(); if (youtubeplayer == null) { getyoutubeplayerprovider(); } else { youtubeplayer.loadvideo(videoid, seektime); } } @override protected void onpause() { super.onpause(); seektime = youtubeplayer.getcurrenttimemillis(); }
it worked fine me, work well. keep coding :)
Comments
Post a Comment