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