Struggling with Youtube Player Support Fragment

前端 未结 2 422
无人共我
无人共我 2020-12-03 02:19

Im trying to use the Youtube Player Support Fragment in a fragment but the app always crash (NullPointerException) and I have not been able to find any similar post to fix i

相关标签:
2条回答
  • 2020-12-03 02:34

    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:

    public static PlayerYouTubeFrag newInstance(String url) {    
        PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();
    
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
    
        playerYouTubeFrag.setArguments(bundle);
    
        playerYouTubeFrag.init(); //This line right here
    
        return 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.

    0 讨论(0)
  • 2020-12-03 02:48

    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:

    public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment {
        private String currentVideoID = "video_id";
        private YouTubePlayer activePlayer;
    
        public static PlayerYouTubeFrag newInstance(String url) {
            PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag();
    
            Bundle bundle = new Bundle();
            bundle.putString("url", url);
    
            playerYouTubeFrag.setArguments(bundle);
    
            return playerYouTubeFrag;
        }
    
        private void init() {
            initialize(DeveloperKey.DEVELOPER_KEY, new OnInitializedListener() {
    
                @Override
                public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) { 
                }
    
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
                    activePlayer = player;
                    activePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
                    if (!wasRestored) {
                        activePlayer.loadVideo(getArguments().getString("url"), 0);
    
                    }
                }
            });
        }
    
        @Override
        public void onYouTubeVideoPaused() {
            activePlayer.pause();
        }
    }
    

    And then call an instance of the fragment like this:

    PlayerYouTubeFrag myFragment = PlayerYouTubeFrag.newInstance("video_id");
    getSupportFragmentManager().beginTransaction().replace(R.id.video_container, myFragment).commit();
    

    Where video_container in my case was an empty frame layout.

    0 讨论(0)
提交回复
热议问题