Show Youtube video within viewpager

前端 未结 3 1225
猫巷女王i
猫巷女王i 2021-01-12 23:40

I want to set YouTube videos within ViewPager. For this I have set FrameLayout in adapter and I have set YoutubeVideoFragment in i

3条回答
  •  伪装坚强ぢ
    2021-01-13 00:12

    public class PlayerFragment extends Fragment implements YouTubePlayer.OnInitializedListener{
    
        YouTubePlayer player;
        Context context;
        YouTubePlayerSupportFragment youTubePlayerSupportFragment;
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            context = getActivity();
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_layout,container,false);
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            youTubePlayerSupportFragment = new YouTubePlayerSupportFragment();
            getChildFragmentManager().beginTransaction().replace(R.id.container,youTubePlayerSupportFragment).commit();
    
        }
    
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
    
            if (!isVisibleToUser && player != null) {
                player.release();
            }
            if (isVisibleToUser && youTubePlayerSupportFragment != null) {
                youTubePlayerSupportFragment.initialize(YOUR_KEY, this);
            }
        }
    
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
            this.player = youTubePlayer;
            this.player.loadVideo("BqVoYfFrCMs");
    
            this.player.play();
        }
    
        @Override
        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
    
        }
    }
    

提交回复
热议问题