SurfaceView height + width gets ignored

后端 未结 2 995
猫巷女王i
猫巷女王i 2020-12-18 10:52

The following script works flawless, but the size of my videoplayer will get f\'d after changing my surfaceview from portrait to landscape. Tried several options like

相关标签:
2条回答
  • 2020-12-18 11:30

    Same problem here:

    I believe the problem is the Vitamio library as I have experienced a similar problem when dealing with the Vitamio SDK. Resizing didn't work for me, when using a SurfaceView and invoking setLayoutParams(ViewGroup.LayoutParams params). The below code works fine using the standard Android media-framework, but importing the vitamio packages breaks it.

    RelativeLayout.LayoutParams video_VIEWLAYOUT = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
    video_VIEWLAYOUT.width = screenWidth;
    video_VIEWLAYOUT.height = (int) (((float)videoHeight / (float)videoWidth) *(float)screenWidth);
    videoView.setLayoutParams(video_VIEWLAYOUT);
    

    My recommendation would be to try using the io.vov.vitamio.widget.VideoView instead of a android.view.SurfaceView.

    0 讨论(0)
  • 2020-12-18 11:33

    use this method and call it inside onConfigurationChanged method :

    private void setVideoSize() {
    
        // // Get the dimensions of the video
    
                // mVideoView is your video view object.
    
        int videoWidth = mVideoView.getVideoWidth();
        int videoHeight = mVideoView.getVideoHeight();
        float videoProportion = (float) videoWidth / (float) videoHeight;
    
        // Get the width of the screen
        int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
        int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
        float screenProportion = (float) screenWidth / (float) screenHeight;
    
        // Get the SurfaceView layout parameters
        android.view.ViewGroup.LayoutParams lp = mVideoView.getLayoutParams();
        if (videoProportion > screenProportion) {
            lp.width = screenWidth;
            lp.height = (int) ((float) screenWidth / videoProportion);
        } else {
            lp.width = (int) (videoProportion * (float) screenHeight);
            lp.height = screenHeight;
        }
        // Commit the layout parameters
        mVideoView.setLayoutParams(lp);
    }
    

    Now on change of orientation your videoview will be resized.

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