Struggling With Youtube Player Support Fragment
Solution 1:
I ran into this problem before and I believe the issue stemmed from trying to inflate the YouTubePlayerSupportFragment
layout. I solved my issue by creating a fragment like this:
publicclassPlayerYouTubeFragextendsYouTubePlayerSupportFragment {
privateString currentVideoID = "video_id";
privateYouTubePlayer activePlayer;
publicstaticPlayerYouTubeFragnewInstance(String url) {
PlayerYouTubeFrag playerYouTubeFrag = newPlayerYouTubeFrag();
Bundle bundle = newBundle();
bundle.putString("url", url);
playerYouTubeFrag.setArguments(bundle);
return playerYouTubeFrag;
}
privatevoidinit() {
initialize(DeveloperKey.DEVELOPER_KEY, newOnInitializedListener() {
@OverridepublicvoidonInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
}
@OverridepublicvoidonInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
activePlayer = player;
activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
if (!wasRestored) {
activePlayer.loadVideo(getArguments().getString("url"), 0);
}
}
});
}
@OverridepublicvoidonYouTubeVideoPaused() {
activePlayer.pause();
}
}
And then call an instance of the fragment like this:
PlayerYouTubeFragmyFragment= PlayerYouTubeFrag.newInstance("video_id");
getSupportFragmentManager().beginTransaction().replace(R.id.video_container, myFragment).commit();
Where video_container
in my case was an empty frame layout.
Solution 2:
CzarMatt nailed it. Just to add to his answer though, don't forget you need to call the init()
method. To do so follow CzarMatt's code and add one line:
publicstatic PlayerYouTubeFrag newInstance(String url) {
PlayerYouTubeFragplayerYouTubeFrag=newPlayerYouTubeFrag();
Bundlebundle=newBundle();
bundle.putString("url", url);
playerYouTubeFrag.setArguments(bundle);
playerYouTubeFrag.init(); //This line right herereturn playerYouTubeFrag;
}
Also, since I'm sure people will run into the same issue I did, even though CzarMatt mentioned it above, just to reiterate, when you pass in a URL for the video, you are NOT passing in the youtube URL
That is, with: "youtube.com/watch?v=z7PYqhABiSo&feature=youtu.be"
You are passing in only the video ID
I.e. use: "z7PYqhABiSo"
Since it is Google's own documentation, it knows how to play it just fine with the id.
Post a Comment for "Struggling With Youtube Player Support Fragment"