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

时间:2015-04-28 12:02:19

标签: android android-orientation android-videoview

我在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);
                }
            }
        });
    }

3 个答案:

答案 0 :(得分:0)

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 ;
    }

}

检查orienChanged中的变量onCreate()是否只有初始时间为假,否则必须为真。

答案 1 :(得分:0)

初始化新配置oncreate ...并使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条件

答案 2 :(得分:0)

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

  1. 实施addVideoListener()

     videoPlayer.addVideoListener(new VideoListener() {
     @Override
     public void onVideoSizeChanged(int width, int height, int 
         unappliedRotationDegrees, float pixelWidthHeightRatio) {
     }
     @Override
     public void onRenderedFirstFrame() {}
     });
    
  2. 根据高度和宽度设置条件

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

    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() {}
     });