android - How to use youtube API? -
my class:
public class mainactivity_for_youtube extend youtubebaseactivity implements youtubeplayer.oninitializedlistener { private static final int recovery_dialog_request = 1; private youtubeplayerview playerview; string fbid=""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // called modify window feature , resize full screen requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.youtubevideo); fbid = getintent().getstringextra("urls"); playerview = (youtubeplayerview) findviewbyid(r.id.player_view); // initializes youtube player view playerview.initialize(config.api_key, this); } // called when youtube player initialization failed @override public void oninitializationfailure(youtubeplayer.provider provider, youtubeinitializationresult errorresult) { // shows dialog if user interaction may fix error if (errorresult.isuserrecoverableerror()) { errorresult.geterrordialog(this, recovery_dialog_request).show(); } else { // displays error occured during initialization string error = string.format( getstring(r.string.error_string), errorresult.tostring()); toast.maketext(this, error, toast.length_long).show(); } } /** * called when initialization of player successful * @param provider provider used initialize player * @param player player instance used control video playback * @param wasrestored depicts whether video restored previous * state. returns true if video resumed last * paused state, else returns false */ @override public void oninitializationsuccess(youtubeplayer.provider provider, youtubeplayer player, boolean wasrestored) { if (!wasrestored) { player.cuevideo("_oea18y8gm0"); player.setplayerstyle(playerstyle.default); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == recovery_dialog_request) { // initializes youtube player view getyoutubeplayerprovider().initialize(config.api_key, this); } } // returns player view defined in xml file private youtubeplayer.provider getyoutubeplayerprovider() { return (youtubeplayerview) findviewbyid(r.id.player_view); } /** * */ private final class eventlistener implements youtubeplayer.playbackeventlistener { /** * called when video starts playing */ @override public void onplaying() { log.e("status", "playing"); } /** * called when video stops playing */ @override public void onpaused() { log.e("status", "paused"); } /** * called when video stopped reason other paused */ @override public void onstopped() { log.e("status", "stopped"); } /** * called when buffering of video starts or ends * @param b true if buffering on, else false */ @override public void onbuffering(boolean b) { } /** * called when jump in video happens. reason can either user scrubbing * or seek method called explicitely * @param */ @override public void onseekto(int i) { } } private final class statechangelistener implements youtubeplayer.playerstatechangelistener { /** * called when player begins loading video. during duration, player * won't accept command may affect video playback */ @override public void onloading() { } /** * called when video loaded. after player can accept * playback affecting commands * @param s video id string */ @override public void onloaded(string s) { } /** * called when youtube ad started */ @override public void onadstarted() { } /** * called when video starts playing */ @override public void onvideostarted() { } /** * called when video ended */ @override public void onvideoended() { } /** * called when kind of error occurs * @param errorreason error string showing reason behind */ @override public void onerror(youtubeplayer.errorreason errorreason) { } }
}
*i have got android key.one thing have done updated android studio 2.0. * have registered on https://console.developers.google.com/apis/library?project=alien-vim-***** , enabled youtube data api.generated sha-1 , got android keys.
getting these errors:
error creating youtubeplayerview com.google.android.youtube.player.internal.w$a: exception thrown invoked constructor in com.google.android.youtube.api.jar.client.remoteembeddedplayer caused by: java.lang.reflect.invocationtargetexception caused by: java.lang.illegalargumentexception: concrete class implementing iobjectwrapper must have *one* declared private field wrapped object. preferably, instance of objectwrapper<t> class. caused by: java.lang.illegalargumentexception: concrete class implementing iobjectwrapper must have *one* declared private field wrapped object. preferably, instance of objectwrapper<t> class.
this simple code, play video:
public class playvideo extends youtubebaseactivity implements youtubeplayer.oninitializedlistener { private youtubeplayer yplayer; private static final string youtubedeveloperkey = "your key"; private static final int recovery_dialog_request = 1; //string film = "http://www.youtube.com/watch?v=0c0pk9fahwa"; public static final string video_id = "0c0pk9fahwa"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layoutplayyoutube); youtubeplayerview youtubeview = (youtubeplayerview) findviewbyid(r.id.youtube_view); youtubeview.initialize(youtubedeveloperkey, this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @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", errorreason.tostring()); toast.maketext(this, errormessage, toast.length_long).show(); } } @override public void oninitializationsuccess(provider provider, youtubeplayer player, boolean wasrestored) { if (!wasrestored) { yplayer = player; /* * variable yplayer global can access * throughout activity, , perform player actions * play, pause , seeking position code. */ yplayer.cuevideo(video_id); } } }
layout.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".playvideo" > <com.google.android.youtube.player.youtubeplayerview android:id="@+id/youtube_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </relativelayout>
Comments
Post a Comment