YouTube

YouTubePlayerFragment

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

Overview

A fragment which 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

[Expand]
Inherited Constants
int TRIM_MEMORY_BACKGROUND
int TRIM_MEMORY_COMPLETE
int TRIM_MEMORY_MODERATE
int TRIM_MEMORY_RUNNING_CRITICAL
int TRIM_MEMORY_RUNNING_LOW
int TRIM_MEMORY_RUNNING_MODERATE
int TRIM_MEMORY_UI_HIDDEN
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.
[Expand]
Inherited methods
void dump(String arg0, FileDescriptor arg1, PrintWriter arg2, String[] arg3)
final boolean equals(Object arg0)
final Activity getActivity()
final Bundle getArguments()
final FragmentManager getFragmentManager()
final int getId()
LoaderManager getLoaderManager()
final Resources getResources()
final boolean getRetainInstance()
final String getString(int arg0)
final String getString(int arg0, Object... arg1)
final String getTag()
final Fragment getTargetFragment()
final int getTargetRequestCode()
final CharSequence getText(int arg0)
boolean getUserVisibleHint()
View getView()
final int hashCode()
static Fragment instantiate(Context arg0, String arg1)
static Fragment instantiate(Context arg0, String arg1, Bundle arg2)
final boolean isAdded()
final boolean isDetached()
final boolean isHidden()
final boolean isInLayout()
final boolean isRemoving()
final boolean isResumed()
final boolean isVisible()
void onActivityCreated(Bundle arg0)
void onActivityResult(int arg0, int arg1, Intent arg2)
void onAttach(Activity arg0)
void onConfigurationChanged(Configuration arg0)
boolean onContextItemSelected(MenuItem arg0)
void onCreate(Bundle arg0)
Animator onCreateAnimator(int arg0, boolean arg1, int arg2)
void onCreateContextMenu(ContextMenu arg0, View arg1, ContextMenu.ContextMenuInfo arg2)
void onCreateOptionsMenu(Menu arg0, MenuInflater arg1)
View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2)
void onDestroy()
void onDestroyOptionsMenu()
void onDestroyView()
void onDetach()
void onHiddenChanged(boolean arg0)
void onInflate(AttributeSet arg0, Bundle arg1)
void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2)
void onLowMemory()
boolean onOptionsItemSelected(MenuItem arg0)
void onOptionsMenuClosed(Menu arg0)
void onPause()
void onPrepareOptionsMenu(Menu arg0)
void onResume()
void onSaveInstanceState(Bundle arg0)
void onStart()
void onStop()
void onTrimMemory(int arg0)
void onViewCreated(View arg0, Bundle arg1)
void registerForContextMenu(View arg0)
void setArguments(Bundle arg0)
void setHasOptionsMenu(boolean arg0)
void setInitialSavedState(Fragment.SavedState arg0)
void setMenuVisibility(boolean arg0)
void setRetainInstance(boolean arg0)
void setTargetFragment(Fragment arg0, int arg1)
void setUserVisibleHint(boolean arg0)
void startActivity(Intent arg0)
void startActivity(Intent arg0, Bundle arg1)
void startActivityForResult(Intent arg0, int arg1)
void startActivityForResult(Intent arg0, int arg1, Bundle arg2)
String toString()
void unregisterForContextMenu(View arg0)
Object clone()
boolean equals(Object arg0)
void finalize()
final Class<?> getClass()
int hashCode()
final void notify()
final void notifyAll()
String toString()
final void wait()
final void wait(long arg0, int arg1)
final void wait(long arg0)
abstract void onConfigurationChanged(Configuration arg0)
abstract void onLowMemory()
abstract void onTrimMemory(int arg0)
abstract void onCreateContextMenu(ContextMenu arg0, View arg1, ContextMenu.ContextMenuInfo arg2)

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 APIs Console.
listener The callbacks that will be invoked when the initialization succeeds or fails.

public static YouTubePlayerFragment newInstance ()

Creates a YouTube fragment.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.