Hide

YouTubePlayerFragment

public class
YouTubePlayerFragment
extends Fragment
implements YouTubePlayer.Provider
java.lang.Object
   ↳ android.app.Fragment
     ↳ com.google.android.youtube.player.YouTubePlayerFragment

Overview

A fragment that contains a YouTubePlayerView. Using this fragment is the preferred way of playing YouTube videos because your activity does not need to extend an activity provided by the library, as is the case with using the YouTubePlayerView directly.

To get started, place this fragment in your view hierarchy and call initialize(String, OnInitializedListener) to create a YouTubePlayer which you can use to load videos into this fragment.

This fragment will save and restore the state of the YouTubePlayer associated with it as part of the onSaveInstanceState/onRestoreInstanceState flow. See the documentation for YouTubePlayer for more details.

The YouTubePlayer associated with this fragment will be released whenever its onDestroyView() method is called. You will therefore have to re-call initialize(String, YouTubePlayer.OnInitializedListener) whenever the activity associated with this fragment is recreated, even if the fragment instance is retained across activity re-creation by setting setRetainInstance(boolean).

It is encouraged that you handle configuration changes caused by orientation in your activity manually, to avoid recreating this fragment's view every time the configuration changes. For the best user experience, you should also manually handle the fullscreen event by changing the layout of your activity. See FULLSCREEN_FLAG_CUSTOM_LAYOUT for more details.

Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay this fragment's view with other views while a video is playing.

If you are targeting a platform which does not support fragments, use YouTubePlayerSupportFragment instead.

Summary

Inherited Constants
Public constructors
YouTubePlayerFragment()
Creates a YouTube fragment.
Public methods
void initialize(String developerKey, YouTubePlayer.OnInitializedListener listener)
Initialize a YouTubePlayer which can be used to play videos and control video playback.
static YouTubePlayerFragment newInstance()
Creates a YouTube fragment.
Inherited methods

Public constructors

public YouTubePlayerFragment ()

Creates a YouTube fragment. This constructor is public only for use by an inflater. Use newInstance() to create a YouTubePlayerFragment programmatically.

Public methods

public void initialize (String developerKey, YouTubePlayer.OnInitializedListener listener)

Initialize a YouTubePlayer which can be used to play videos and control video playback. One of the callbacks in listener will be invoked when the initialization succeeds or fails.

Parameters
developerKey A valid API key which is enabled to use the YouTube Data API v3 service. To generate a new key, visit the Google Developers Console.
listener The callbacks that will be invoked when the initialization succeeds or fails.

public static YouTubePlayerFragment newInstance ()

Creates a YouTube fragment.