Video Is Not Playing In Youtubeplayer Fragment
I am making an app which uses Youtube player fragment. It is loaded but video is not getting played. Here is my code: Youtube.java (Fragment) public class Youtube extends YouTubePl
Solution 1:
I think you should write the code in official way. Take look at here, and download the zip file(sample applications included in the YouTubeAndroidAPIDemo
package) here to get the sample. And take look at FragmentDemoActivity
.
Basically, you need to do the Activity
which include YoutubePlayerFragment
as follows:
publicclassFragmentDemoActivityextendsYouTubeFailureRecoveryActivity {
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragments_demo);
YouTubePlayerFragment youTubePlayerFragment =
(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
}
@OverridepublicvoidonInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
player.cueVideo("nCgQDjiotG0");
}
}
@OverrideprotectedYouTubePlayer.ProvidergetYouTubePlayerProvider() {
return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
}
}
YouTubeFailureRecoveryActivity :
publicabstractclassYouTubeFailureRecoveryActivityextendsYouTubeBaseActivityimplementsYouTubePlayer.OnInitializedListener {
privatestaticfinalintRECOVERY_DIALOG_REQUEST=1;
@OverridepublicvoidonInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
StringerrorMessage= String.format(getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY, this);
}
}
protectedabstract YouTubePlayer.Provider getYouTubePlayerProvider();
}
And the fragments_demo.xml
:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><fragmentandroid:name="com.google.android.youtube.player.YouTubePlayerFragment"android:id="@+id/youtube_fragment"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>
Post a Comment for "Video Is Not Playing In Youtubeplayer Fragment"