public void setFullscreen ( final boolean fullscreen ) throws RuntimeException { if ( mediaPlayer == null ) throw new RuntimeException ( "Media Player is not initialized" ) ; if ( this . currentState != State . ERROR ) { if ( FullscreenVideoView . this . fullscreen == fullscreen ) return ; FullscreenVideoView . this . fullscreen = fullscreen ; final boolean wasPlaying = mediaPlayer . isPlaying ( ) ; if ( wasPlaying ) pause ( ) ; if ( FullscreenVideoView . this . fullscreen ) { if ( activity != null ) activity . setRequestedOrientation ( ActivityInfo . SCREEN_ORIENTATION_UNSPECIFIED ) ; View rootView = getRootView ( ) ; View v = rootView . findViewById ( android . R . id . content ) ; ViewParent viewParent = getParent ( ) ; if ( viewParent instanceof ViewGroup ) { if ( parentView == null ) parentView = ( ViewGroup ) viewParent ; // Prevents MediaPlayer to became invalidated and released detachedByFullscreen = true ; // Saves the last state (LayoutParams) of view to restore after currentLayoutParams = FullscreenVideoView . this . getLayoutParams ( ) ; parentView . removeView ( FullscreenVideoView . this ) ; } else Log . e ( TAG , "Parent View is not a ViewGroup" ) ; if ( v instanceof ViewGroup ) { ( ( ViewGroup ) v ) . addView ( FullscreenVideoView . this ) ; } else Log . e ( TAG , "RootView is not a ViewGroup" ) ; } else { if ( activity != null ) activity . setRequestedOrientation ( initialConfigOrientation ) ; ViewParent viewParent = getParent ( ) ; if ( viewParent instanceof ViewGroup ) { // Check if parent view is still available boolean parentHasParent = false ; if ( parentView != null && parentView . getParent ( ) != null ) { parentHasParent = true ; detachedByFullscreen = true ; } ( ( ViewGroup ) viewParent ) . removeView ( FullscreenVideoView . this ) ; if ( parentHasParent ) { parentView . addView ( FullscreenVideoView . this ) ; FullscreenVideoView . this . setLayoutParams ( currentLayoutParams ) ; } } } resize ( ) ; Handler handler = new Handler ( Looper . getMainLooper ( ) ) ; handler . post ( new Runnable ( ) { @ Override public void run ( ) { if ( wasPlaying && mediaPlayer != null ) start ( ) ; } } ) ; } }
Turn VideoView fulllscreen mode on or off .