How to play a video with the YouTube API using YouTubePlayerFragment?

13,638

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:

public class VideoFragment extends YouTubePlayerSupportFragment {

    public VideoFragment() { }

    public static VideoFragment newInstance(String url) {

        VideoFragment f = new VideoFragment();

        Bundle b = new Bundle();
        b.putString("url", url);

        f.setArguments(b);
        f.init();

        return f;
    }

    private void init() {

        initialize("yourapikey", new OnInitializedListener() {

            @Override
            public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { }

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                if (!wasRestored) { 
                    player.cueVideo(getArguments().getString("url"));
                }
            }
        });
    }
}

In code it could look like this:

VideoFragment f = 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.

Share:
13,638
kort.es
Author by

kort.es

Im learning.

Updated on July 21, 2022

Comments

  • kort.es
    kort.es almost 2 years

    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: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.

    This is my .xml:

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    And this is the class:

    public class YoutubeFragment extends YoutubeFailureRecovery {
    
        YouTubePlayer player;
        YouTubePlayerView playerView;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle arg2) {
            // TODO Auto-generated method stub
            return inflater.inflate(R.layout.detailview, container, false);
        }
    
        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            playerView = (YouTubePlayerView)getActivity().findViewById(R.id.player);
            playerView.initialize(DataHolder.DEVELOPER_KEY, this);
    
        }
    
        @Override
        public void onInitializationSuccess(Provider provider, YouTubePlayer player,
                                            boolean wasRestored) {
            // TODO Auto-generated method stub
            player.cueVideo("nCgQDjiotG0");
        }
    
        @Override
        protected Provider getYouTubePlayerProvider() {
            // TODO Auto-generated method stub
            return playerView;
        }
    }
    


    public abstract class YoutubeFailureRecovery extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener{
    

    I just don't know what to do. I tried to extend my Fragment class with 'YoutubePlayerSupportFragment', tried to add a video-like fragment in XML, but nothing is working (error inflating). Maybe someone has some experience with using YouTube API in Fragment?

  • DJElbow
    DJElbow over 9 years
    @Philipp, I am having a similar issue here, goo.gl/V55wsu . I would appreciate any advice you have.
  • Nicolás Arias
    Nicolás Arias about 9 years
    When I rotate the screen the video just dissapears. Any suggestions?
  • Aayushi
    Aayushi about 9 years
    Thanks this helped me but how can i make this fragment to use full parent view
  • Ravi
    Ravi over 8 years
    @Phipipp You are a Genius
  • wax911
    wax911 over 7 years
    Still works in 2016!! :D the google sample doesn't really help. Thank you so much
  • gamerounet
    gamerounet almost 7 years
    Can we display 2 video in the same activity with this solution? I mean for example: VideoFragment f = VideoFragment.newInstance("url"); getSupportFragmentManager().beginTransaction().replace(R.id.‌​fragment_container, f).commit() VideoFragment f2 = VideoFragment.newInstance("url2l"); getSupportFragmentManager().beginTransaction().replace(R.id.‌​fragment_container, f2).commit()
  • Alan Nelson
    Alan Nelson over 5 years
    url didn't work for me. I had to use "videoId" in place of the URL