Fullscreen the Exoplayer

前端 未结 9 1489
一向
一向 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:10

    Exoplayer doesn't provide the fullscreen so here is the workaround that worked for me. Here I have restricted the Screen to rotate and manually changing the orientation programmatically change the width and height of the player_view and has set the visibility of the toolbar gone. Used Data Binding and Kotlin.

    Follows a code block for XML

    
    
        
    
    
            
    
            
    
                
    
                
            
    
            
    
                
    
                
    
    
            
         
        
    

    Changes in Android Manifest

    
    

    Check the orientation and rotate it via the following code

    mBinding.playerView.exo_fullscreen_btn.setOnClickListener {
            if ((activity as TrainingVideoActivity).checkLandscapeOrientation()) {
                (activity as TrainingVideoActivity).changeOrientationToLandscape(false)
    
            } else {
                (activity as TrainingVideoActivity).changeOrientationToLandscape(true)
    
            }
    
        }
    

    Methods Signature are following

    /**
     * Changes the Orientation
     * @param shouldLandscape
     */
    fun changeOrientationToLandscape(shouldLandscape: Boolean) {
        requestedOrientation = if (shouldLandscape) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }
    
    /**
     * Checks the Orientation
     * And returns true if Landscape else false
     */
    fun checkLandscapeOrientation() : Boolean {
        val orientation = resources.configuration.orientation
        return orientation == Configuration.ORIENTATION_LANDSCAPE
    }
    

    Now Just Override onConfigurationChanged method in your fragment/activity, since here I have used Fragment. So here I have changed the width/height of the parent container in which ExoplayerView is placed.

    /**
     * Used for Showing Video on Full Screen
     * @param newConfig
     * Used EXO_PLAYER_VIEW_HEIGHT as 250
     */
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            hideToolbarAndShowFullScreen()
            mBinding.playerView.exo_fullscreen_btn.setImageDrawable(ContextCompat.getDrawable(activity!!, R.drawable.ic_shrink))
            val params = mBinding.videoPlayerContainer.layoutParams as ConstraintLayout.LayoutParams
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            params.height = ViewGroup.LayoutParams.MATCH_PARENT
            mBinding.videoPlayerContainer.layoutParams = params
        } else {
            showToolbarAndClearFullScreen()
            mBinding.playerView.exo_fullscreen_btn.setImageDrawable(ContextCompat.getDrawable(activity!!, R.drawable.ic_fullscreen))
            val params = mBinding.videoPlayerContainer.layoutParams as ConstraintLayout.LayoutParams
            val factor = mBinding.playerView.context.resources.displayMetrics.density
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            params.height = (EXO_PLAYER_VIEW_HEIGHT * factor).toInt()
            mBinding.videoPlayerContainer.layoutParams = params
        }
    }
    
    /**
     * Show the Toolbar and reset to original in the Portrait Mode
     */
    private fun showToolbarAndClearFullScreen() {
        (activity as TrainingVideoActivity).supportActionBar!!.show()
        (activity as TrainingVideoActivity).window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
    
    }
    

    Lastly, XML for the player_controller

    
    
    
    
    
    
        
    
            
    
            
        
    
        
    
            
    
                
    
                
    
                
    
                
    
            
        
    
    
    

    Let me know if this works.

提交回复
热议问题