2015-04-28 79 views
0

我使用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); 
       } 
      } 
     }); 
    } 
+0

如果(videoview.getVideoWidth()> videoview.getVideoHeight())检查此条件 – Android

+0

这种情况正常工作。请参阅OnPrepared方法。 – user2920014

+0

您检查MediaPlayer的hieght和宽度......做同样的事情videoview – Android

回答

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

} 

检查变量orienChangedonCreate()只有初始时间这将是错误的,oth否则它一定是真的。

0

OnCreate中初始化新的配置...,并newConfigOD valiable全球。

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删除此()

+0

但我必须得到视频大小(高度和宽度)而不是videoview的大小(高度和宽度),加载视频大小(高度和宽度)需要几秒钟,因为它从服务器加载。假设我运行我的代码,进度条会显示,直到它加载视频,然后如果视频加载完成,我会检查视频的尺寸,然后如果高度大于宽度,那么它将正常工作,但如果宽度大于高度那么我将不得不将屏幕方向改变为横向模式,但在这种情况下,onCreate方法将再次调用,并且再次进度条会显示哪个已经显示第一个 – user2920014

相关问题