Struggling with Youtube Player Support Fragment

前端 未结 2 452
无人共我
无人共我 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.

提交回复
热议问题