Android 如何根据视频设置屏幕方向';s的高度和宽度

Android 如何根据视频设置屏幕方向';s的高度和宽度,android,android-orientation,android-videoview,Android,Android Orientation,Android Videoview,我正在使用videoview,在onPrepared方法中,它返回媒体播放器的高度和宽度。然后我检查视频的宽度是否大于高度,并旋转屏幕。问题是,如果我旋转屏幕方向,它会再次调用onCreate方法,然后调用所有代码,因此需要时间再次初始化视频 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout f

我正在使用
videoview
,在
onPrepared
方法中,它返回媒体播放器的高度和宽度。然后我检查视频的宽度是否大于高度,并旋转屏幕。问题是,如果我旋转屏幕方向,它会再次调用
onCreate
方法,然后调用所有代码,因此需要时间再次初始化视频

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the layout from video_main.xml
        setContentView(R.layout.play_video_activity);

        // Insert your Video URL
        String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";

        // Find your VideoView in your video_main.xml layout
        final VideoView videoview = (VideoView) findViewById(R.id.VideoView);
        // Execute StreamVideo AsyncTask

        // Create a progressbar
        final ProgressDialog pDialog;
        pDialog = new ProgressDialog(PlayVideoActivity.this);
        // Set progressbar title
        pDialog.setTitle("Android Video Streaming Tutorial");
        // Set progressbar message
        pDialog.setMessage("Buffering...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        // Show progressbar
        pDialog.show();

        try {
            // Start the MediaController
            MediaController mediacontroller = new MediaController(
                    PlayVideoActivity.this);
            mediacontroller.setAnchorView(videoview);
            // Get the URL from String VideoURL
            Uri video = Uri.parse(VideoURL);
            videoview.setMediaController(mediacontroller);
            videoview.setVideoURI(video);

        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

        videoview.requestFocus();
        videoview.setOnPreparedListener(new OnPreparedListener() {
            // Close the progress bar and play the video
            public void onPrepared(MediaPlayer mp) {
                pDialog.dismiss();
                videoview.start();

                if(mp.getVideoWidth()> mp.getVideoHeight())
                {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                }
            }
        });
    }

向清单文件中的活动添加
android:configChanges=“orientation”

在活动中声明变量

boolean orienChanged = false;
int lastOrientation = 0;
覆盖方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int orientation = newConfig.orientation;
    if(orientation != lastOrientation){
       orienChanged  = true;
       lastOrientation = orientation ;
    }

}

onCreate()
中检查变量
orientChanged
,仅在初始时间它将为false,否则它必须为true。

在创建时初始化新配置。。。并使newConfigOD在全球范围内有效

 Configuration newConfigOD = getResources().getConfiguration();    
然后重写此方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if(videoview.getVideoWidth()> videoview.getVideoHeight())  {
        if (newConfigOD.orientation != Configuration.ORIENTATION_LANDSCAPE) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } 
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

试试这个。。。这应该按照你的要求。并从onCreate()中删除此if条件。

根据视频比例自动设置方向

  • 实现addVideoListener()

  • 根据高度和宽度设置条件

     if(width>height){
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
     }
     if(height>width){
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
     }
    
  • 你的最终目标是

    videoPlayer.addVideoListener(new VideoListener() {
         @Override
         public void onVideoSizeChanged(int width, int height, int 
               unappliedRotationDegrees, float pixelWidthHeightRatio) { 
             if(width>height){
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
             }
             if(height>width){
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
             }
        }
        @Override
        public void onRenderedFirstFrame() {}
     });
    

  • 如果(videoview.getVideoWidth()>videoview.getVideoHeight())检查此条件此条件工作正常。请参阅OnPrepared方法。检查mediaplayer的高度和宽度。。。对VideoViews执行相同的操作,但问题是假设视频宽度为480,高度为640,那么我将以纵向模式(默认情况下)显示视频,但如果宽度为640,高度为480,则我必须在横向模式下显示视频,但在这种情况下,如果我在横向模式下旋转视频,它将再次初始化视频,因为活动再次调用创建方法,但我必须获得视频大小(高度和宽度),而不是视频视图大小(高度和宽度),加载视频大小需要几秒钟(高度和宽度)因为它是从服务器加载的。假设我运行我的代码,进度条将显示,直到加载视频,然后如果视频加载完成,我将检查视频的尺寸,然后如果高度大于宽度,它将正常工作,但如果宽度大于高度,我将不得不将屏幕方向更改为横向模式,但在这种情况下case onCreate方法将再次调用,并且进度条将再次显示已首先显示的内容
    videoPlayer.addVideoListener(new VideoListener() {
         @Override
         public void onVideoSizeChanged(int width, int height, int 
               unappliedRotationDegrees, float pixelWidthHeightRatio) { 
             if(width>height){
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
             }
             if(height>width){
                 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
             }
        }
        @Override
        public void onRenderedFirstFrame() {}
     });