Show Youtube video within viewpager

前端 未结 3 1224
猫巷女王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:03

    Inspired by the answer above, I have a working solution for this problem.

    In OnCreate():

    mYouTubePlayerSupportFragment = YouTubePlayerSupportFragment.newInstance();
        if (getUserVisibleHint()) {
            Log.v (TAG, "Committing transaction, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL));
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.replace(R.id.fl_youtube_player, mYouTubePlayerSupportFragment).commit();
            mYouTubePlayerSupportFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
        }
    

    In setUserVisibleHint():

    if (!isVisibleToUser && mYoutubePlayer != null) {
        Log.v (TAG, "Releasing youtube player, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL));
        mYoutubePlayer.release();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.remove(mYouTubePlayerSupportFragment).commit();
    }
    if (isVisibleToUser && mYouTubePlayerSupportFragment != null) {
        Log.v (TAG, "Initializing youtube player, URL : " + getArguments().getString(KeyConstant.KEY_VIDEO_URL));
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_youtube_player, mYouTubePlayerSupportFragment).commit();
        mYouTubePlayerSupportFragment.initialize(DeveloperKey.DEVELOPER_KEY, this);
    }
    
    0 讨论(0)
  • 2021-01-13 00:05

    I got the error that the YouTube view is overlayed by my view pager when the pager contains multiple YouTubePlayerFragments.

    After a lot of research, I solved it as follows.

    • Override setUserVisibleHint() of the fragment.

    • Release the previous YouTubePlayer (mYoutubePlayer).

    • Create a new instance of the player by initialize().

    • In onInitilizationSuccess, load the player with the youtube_video_id.

    Note: I am using FragmentStatePagerAdapter.

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    
        if (!isVisibleToUser && mYoutubePlayer != null) {
            mYoutubePlayer.release();
        }
        if (isVisibleToUser && mYouTubePlayerSupportFragment != null) {
            mYouTubePlayerSupportFragment.initialize(API_KEY, Context);
        }
    
    0 讨论(0)
  • 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) {
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题