how to implemenent android video player in full screen

筅森魡賤 提交于 2019-11-30 21:39:58

This is demo custom media control, but full screen and small screen no code. http://www.brightec.co.uk/blog/custom-android-media-controller I'm create method setFullScreen. Code below working for me.

private boolean mFullScreen = true;

@Override
public boolean isFullScreen() {   
    if(mFullScreen){    
        Log.v("FullScreen", "--set icon full screen--");
        return false;
    }else{
        Log.v("FullScreen", "--set icon small full screen--");
        return true;
    }   
}
@Override
public void toggleFullScreen() {
    Log.v("FullScreen", "-----------------click toggleFullScreen-----------");
    setFullScreen(isFullScreen());

}
// End VideoMediaController.MediaPlayerControl

public void setFullScreen(boolean fullScreen){
    fullScreen = false;

    if (mFullScreen)

    {
        Log.v("FullScreen", "-----------Set full screen SCREEN_ORIENTATION_LANDSCAPE------------"); 
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
        DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          int height = displaymetrics.heightPixels;
          int width = displaymetrics.widthPixels;
          android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
          params.width = width;
          params.height=height;
          params.setMargins(0, 0, 0, 0);
          //set icon is full screen
         mFullScreen = fullScreen;
    }
    else{
        Log.v("FullScreen", "-----------Set small screen SCREEN_ORIENTATION_PORTRAIT------------");             
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);      
        DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          final FrameLayout mFrame = (FrameLayout) findViewById(R.id.videoSurfaceContainer);
         // int height = displaymetrics.heightPixels;
          int height = mFrame.getHeight();//get height Frame Container video
          int width = displaymetrics.widthPixels;
          android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
          params.width = width;
          params.height= height;
          params.setMargins(0, 0, 0, 0);
          //set icon is small screen
          mFullScreen = !fullScreen;
    }
}

or use onConfigurationChanged for Rotation screen.

*@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

  // Checks the orientation of the screen
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
   // setContentView(R.layout.view_lang);
      DisplayMetrics displaymetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      int height = displaymetrics.heightPixels;
      int width = displaymetrics.widthPixels;
      android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
      params.width = width;
      params.height=height;// -80 for android controls
      params.setMargins(0, 0, 0, 0);
  } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    //setContentView(R.layout.view);    
    DisplayMetrics displaymetrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      int height = displaymetrics.heightPixels;
      int width = displaymetrics.widthPixels;
      android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
      params.width = width;
      params.height=height / 3;
      params.setMargins(0, 0, 0, 0);
  }
}

add code here in AndroidManifest.xml

 android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 

Sorry English !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!