how to implemenent android video player in full screen

后端 未结 1 1873
Happy的楠姐
Happy的楠姐 2021-01-13 01:26

i have implemented android video player , but i don\'t need like this how i need mean when opening activity i need to pay video half screen in center when i click button ful

1条回答
  •  旧时难觅i
    2021-01-13 02:07

    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 !

    0 讨论(0)
提交回复
热议问题