Fullscreen the Exoplayer

前端 未结 9 1759
一向
一向 2021-02-02 16:05

I try to show the show video (.mp4) with exoplayer in RecyclerView and ViewPager. I show the video controller with custom layo

9条回答
  •  忘掉有多难
    2021-02-02 17:06

    This might be too late but this might help other developers.

    Exoplayer doesn't provide the fullscreen functionality by-default. So we need a workaround for this. We can implement this fullscreen functionality using a dialog. We just need to remove and add the Playerview from our activity to the dialog.

    mFullScreenDialog = new Dialog(mContext, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
            public void onBackPressed() {
                if (mExoPlayerFullscreen) {   //mExoPlayerFullscreen is a boolean that we need to maintain to know whether screen is fullscreen or not.
                    ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                    closeFullscreenDialog();
                }
                super.onBackPressed();
            }
        };
    

    Here we initialized the dialog.

    mFullScreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mExoPlayerFullscreen) {
                    ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                    openFullscreenDialog();
                } else {
                    ((Activity) mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                    closeFullscreenDialog();
                }
            }
        });
    

    Here we initialized the fullscreen button of out exoplayer.

    private void openFullscreenDialog() {
        ((ViewGroup) playerView.getParent()).removeView(playerView);
        mFullScreenDialog.addContentView(playerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mFullScreenIcon.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.exoplayer_shrink));
        mExoPlayerFullscreen = true;
        mFullScreenDialog.show();
    }
    
    private void closeFullscreenDialog() {
        ((ViewGroup) playerView.getParent()).removeView(playerView);
        ((FrameLayout) findViewById(R.id.main_media_frame)).addView(playerView);
        mExoPlayerFullscreen = false;
        mFullScreenDialog.dismiss();
        mFullScreenIcon.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.exoplayer_expand));
    }
    

    playerview is the Playerview that you will initialize for

    
    
        
    
    

    and in java,

    playerView = (PlayerView) findViewById(R.id.player_view);
    

提交回复
热议问题