2015-04-28 82 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

相關問題