I want to set YouTube videos within ViewPager
. For this I have set FrameLayout
in adapter and I have set YoutubeVideoFragment in i
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) {
}
}