How To Play A Video With The Youtube Api Using Youtubeplayerfragment?
I'm trying to play video in my Fragment. However, I cannot get it to work. If I'm extending 'YoutubeFailureRecovery' I get: 09-06 21:56:40.472: E/AndroidRuntime(4946): Caused by:
Solution 1:
The YouTubePlayerView only works with an Activity that extends YouTubeBaseActivity.
If you want to use Fragments you have to use the YoutubePlayerFragment / SupportFragment.
You can for example create your custom Fragment that inherits from YoutubePlayerSupportFragment:
publicclassVideoFragmentextendsYouTubePlayerSupportFragment {
publicVideoFragment() { }
publicstaticVideoFragmentnewInstance(String url) {
VideoFragment f = newVideoFragment();
Bundle b = newBundle();
b.putString("url", url);
f.setArguments(b);
f.init();
return f;
}
privatevoidinit() {
initialize("yourapikey", newOnInitializedListener() {
@OverridepublicvoidonInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }
@OverridepublicvoidonInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(getArguments().getString("url"));
}
}
});
}
}
In code it could look like this:
VideoFragmentf= VideoFragment.newInstance("your-video-url");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).commit();
The "fragment_container" in this case should be an empty FrameLayout.
Post a Comment for "How To Play A Video With The Youtube Api Using Youtubeplayerfragment?"