PierfrancescoSoffritti / android-youtube-player

YouTube Player library for Android and Chromecast, stable and customizable.
https://pierfrancescosoffritti.github.io/android-youtube-player/
MIT License
3.41k stars 756 forks source link

FullScreen not work in RecyclerView #699

Open pavlovpavlo opened 3 years ago

pavlovpavlo commented 3 years ago

Hello, I am using YouTubePlayerView in my RecyclerView, but when I click on the full screen button, it does not open completely, but only increases by a couple of pixels, why can this be?

umer-sufyan commented 3 years ago

I am facing same problem

umer-sufyan commented 3 years ago

Well I fixed that by following sample code guide lines there are couple of things that you need to consider to make sure it works as full screen

public class YoutubeVideoAdapter extends RecyclerView.Adapter {

ArrayList<VideoLink> videosList = new ArrayList<>();
Context context;
boolean isFromHomeFrag;
private Lifecycle lifecycle;
private FullScreenHelper fullScreenHelper;
private String currentVideoId;

public YoutubeVideoAdapter(Context context, ArrayList<VideoLink> videosList, boolean isFromHomeFrag, Lifecycle lifecycle) {
    this.context = context;
    this.videosList = videosList;
    this.isFromHomeFrag = isFromHomeFrag;
    this.lifecycle = lifecycle;
    this.fullScreenHelper = new FullScreenHelper(((HomeActivity)context));
}

@Override
public YoutubeVideoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;

    itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.videos_single_item, parent, false);

    YouTubePlayerView youTubePlayerView = itemView.findViewById(R.id.youtube_player_view);

    lifecycle.addObserver(youTubePlayerView);

    return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull YoutubeVideoAdapter.ViewHolder holder, int position) {
    final String videoId = getItem(position).getLink();

    holder.cueVideo(videoId);

}

@Override
public int getItemCount() {
    return videosList.size();
}

public VideoLink getItem(int position) {
    return videosList.get(position);
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private YouTubePlayerView youTubePlayerView;
    private YouTubePlayer youTubePlayer;

    public ViewHolder(View view) {
        super(view);
        youTubePlayerView = view.findViewById(R.id.youtube_player_view);

        youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
            @Override
            public void onReady(@NonNull YouTubePlayer initializedYouTubePlayer) {
                youTubePlayer = initializedYouTubePlayer;
                youTubePlayer.cueVideo(currentVideoId, 0);
            }
        });

        addFullScreenListenerToPlayer(youTubePlayerView);
    }

    void cueVideo(String videoId) {
        currentVideoId = videoId;

        if(youTubePlayer == null)
            return;

        youTubePlayer.cueVideo(videoId, 0);
    }
}

public void add(VideoLink item) {
    videosList.add(item);
    notifyItemInserted(videosList.size() - 1);
}

public void addAll(List<VideoLink> items) {
    for (VideoLink item : items) {
        add(item);
    }
}

public void remove(VideoLink item) {
    int position = videosList.indexOf(item);
    if (position > -1) {
        videosList.remove(position);
        notifyItemRemoved(position);
    }
}

public void removeAll(List<VideoLink> items) {
    for (VideoLink item : items) {
        remove(item);
    }
}

private void addFullScreenListenerToPlayer(YouTubePlayerView youTubePlayerView) {
    youTubePlayerView.addFullScreenListener(new YouTubePlayerFullScreenListener() {
        @Override
        public void onYouTubePlayerEnterFullScreen() {

           //**To Show Video in same recyclerView here you need to make sure you did not apply padding or margin to have space within recycler item since in landscape mode that would be considering as well** 
            ((HomeActivity)context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            fullScreenHelper.enterFullScreen();

            addCustomActionsToPlayer(youTubePlayerView);

        **// second way is that pass video id to Activity and call Fullscreen from there programatically**

         /*
            Bundle bundle = new Bundle();
            bundle.putString(Constants.KEY_VIDEO_ID, currentVideoId);

            Intent i = new Intent(getApplicationContext(), EnterFullScreenActivity.class);
            i.putExtras(bundle);
            context.startActivity(i);
          */

        }

        @Override
        public void onYouTubePlayerExitFullScreen() {
            ((HomeActivity)context).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            fullScreenHelper.exitFullScreen();

            removeCustomActionsFromPlayer(youTubePlayerView);
        }
    });
}

/**
 * This method adds a new custom action to the player.
 * Custom actions are shown next to the Play/Pause button in the middle of the player.
 */
private void addCustomActionsToPlayer(YouTubePlayerView youTubePlayerView) {
    Drawable customAction1Icon = ContextCompat.getDrawable(context, R.drawable.ic_fast_rewind_white_24dp);
    Drawable customAction2Icon = ContextCompat.getDrawable(context, R.drawable.ic_fast_forward_white_24dp);
    assert customAction1Icon != null;
    assert customAction2Icon != null;

    youTubePlayerView.getPlayerUiController().setCustomAction1(customAction1Icon, view ->
            Toast.makeText(context, "custom action1 clicked", Toast.LENGTH_SHORT).show());

    youTubePlayerView.getPlayerUiController().setCustomAction2(customAction2Icon, view ->
            Toast.makeText(context, "custom action1 clicked", Toast.LENGTH_SHORT).show());
}

private void removeCustomActionsFromPlayer(YouTubePlayerView youTubePlayerView) {
    youTubePlayerView.getPlayerUiController().showCustomAction1(false);
    youTubePlayerView.getPlayerUiController().showCustomAction2(false);
}

}

Hope it helps some one, same way could be used for fragment Fullscreen