android - Reload youtubeplayer in activity? -
please me,
i want when user click item in listview -> reload activity video clicked (like youtube app android)
pleale me idea. thanks
update source code:
public class youtubeplayeractivity extends appcompatactivity implements youtubeplayer.oninitializedlistener {
private static final int recovery_dialog_request = 1; youtubeplayerfragment myyoutubeplayerfragment; private toolbar mtoolbar; private string name; private string countview; private string youtubeid = "fhwaji1hsfo"; private string cate; private recyclerview mrecyclerview; private linearlayoutmanager mlayoutmanager; private clipadapter mclipadapter; private list<clipitemdata> clipitemdatas = new arraylist<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_youtube_player); mrecyclerview = (recyclerview) findviewbyid(r.id.youtube_recycler_view); // use setting improve performance if know changes // in content not change layout size of recyclerview mrecyclerview.sethasfixedsize(true); // use linear layout manager mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); final linearlayoutmanager mlayoutmanager; mlayoutmanager = new linearlayoutmanager(this); mrecyclerview.setlayoutmanager(mlayoutmanager); (int = 0; < 50; i++) { clipitemdata clipitemdata = new clipitemdata("to0tju75ugg", "test", "123", "xe", "http://img.youtube.com/vi/" + "to0tju75ugg" + "/0.jpg"); clipitemdatas.add(clipitemdata); } // specify adapter (see next example) mclipadapter = new clipadapter(this, clipitemdatas); mrecyclerview.setadapter(mclipadapter); bundle getbundle = null; getbundle = this.getintent().getextras(); youtubeid = getbundle.getstring("id"); name = getbundle.getstring("name"); countview = getbundle.getstring("count_view"); cate = getbundle.getstring("cate"); myyoutubeplayerfragment = (youtubeplayerfragment) getfragmentmanager() .findfragmentbyid(r.id.youtubeplayerfragment); myyoutubeplayerfragment.initialize(appconfig.youtube_android_key, this); mtoolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(mtoolbar); getsupportactionbar().setdisplayhomeasupenabled(true); mrecyclerview.addonitemtouchlistener(new recycleritemclicklistener(this, mrecyclerview, new recycleritemclicklistener.onitemclicklistener() { @override public void onitemclick(view view, int position) { youtubeid = "to0tju75ugg"; toast.maketext(getapplicationcontext(), youtubeid, toast.length_short).show(); } @override public void onitemlongclick(view view, int position) { } })); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { // respond action bar's up/home button case android.r.id.home: super.onbackpressed(); return true; } return super.onoptionsitemselected(item); } @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer youtubeplayer, boolean wasrestored) { if (!wasrestored) { youtubeplayer.loadvideo(youtubeid); } } @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult errorreason) { if (errorreason.isuserrecoverableerror()) { errorreason.geterrordialog(this, recovery_dialog_request).show(); } else { string errormessage = string.format( "there error initializing youtubeplayer (%1$s)", errorreason.tostring()); toast.maketext(this, errormessage, toast.length_long).show(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == recovery_dialog_request) { // retry initialization if user performed recovery action getyoutubeplayerprovider().initialize(appconfig.youtube_android_key, this); } } protected youtubeplayer.provider getyoutubeplayerprovider() { return (youtubeplayerview) findviewbyid(r.id.youtubeplayerfragment); }
}
this can done in different ways.
you can use simple webview load videos.
you can use official "youtube android player api"
implementing youtube's api easy,
first create youtubeplayerview
in xml,
<com.google.android.youtube.player.youtubeplayerview android:id="@+id/youtubeplayerview" android:layout_width="match_parent" android:layout_height="wrap_content"/>
then show video this,
public class mainactivity extends youtubebaseactivity implements youtubeplayer.oninitializedlistener{ public static final string api_key = "your_api_key"; public static final string video_id = "your_video_id"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); youtubeplayerview youtubeplayerview = (youtubeplayerview)findviewbyid(r.id.youtubeplayerview); youtubeplayerview.initialize(api_key, this); } @override public void oninitializationfailure(provider provider, youtubeinitializationresult result) { toast.maketext(getapplicationcontext(), "oninitializationfailure()", toast.length_long).show(); } @override public void oninitializationsuccess(provider provider, youtubeplayer player, boolean wasrestored) { if (!wasrestored) { player.cuevideo(video_id); } } }
Comments
Post a Comment