2015-03-02 86 views
1

我有一個視頻播放器。並且想要啓用橫向和縱向定位。但問題是玩家每次下載文件的方向發生變化時都會下載文件。 我試過使用「onCofigurationChange」,但它仍然不起作用。 我該如何解決問題?視頻播放器中的Android方向更改

P.S.我已經更改了我的AndroidManifest.xml文件並添加了

android:configChanges="orientation|screenSize|keyboardHidden" 

所以問題出在我的java文件中的某處。

這裏是我的代碼:

public class VideoViewActivity extends Activity { 

ProgressDialog pDialog; 
VideoView videoview; 

String VideoURL = "http://v.mover.uz/ep83Sfim_s.mp4"; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.videoview_main); 

    if (Build.VERSION.SDK_INT < 16) { 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    } 
    else 
    { 
     View decorView = getWindow().getDecorView(); 
    // Hide the status bar. 
     int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; 
     decorView.setSystemUiVisibility(uiOptions); 
    // Remember that you should never show the action bar if the status bar is hidden, so hide that too if necessary. 
     ActionBar actionBar = getActionBar(); 
     actionBar.hide(); 
    } 

    //  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 


    setupActivity(); 

} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    setContentView(R.layout.videoview_main); 

    setupActivity(); 
} 

public void setupActivity() { 
    videoview = (VideoView) findViewById(R.id.VideoView); 
      // Create a progressbar 
    pDialog = new ProgressDialog(VideoViewActivity.this); 
    // Set progressbar message 
    pDialog.setMessage("Buffering..."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(false); 
    // Show progressbar 
    pDialog.show(); 

    try { 
     // Start the MediaController 
     MediaController mediacontroller = new MediaController(
       VideoViewActivity.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(); 
     } 
    }); 
    } 
} 
+0

我想你應該在你的活動的onDestroy中添加一些代碼。因爲每次屏幕方向改變,活動將首先被破壞 – 2015-03-02 10:01:27

+0

只需從onConfigurationChanged方法中刪除你的代碼,並在超級getWindow()後添加簡單的setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);在that.it工作。 – 2015-03-02 10:06:47

+0

我試過了,但它不起作用(( – Ugadaykin 2015-03-02 11:30:08

回答

0

在你onConfigurationChanged應該是:

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 

    //if you need change layout then do: 
    switch(newConfig.orientation) { 
     case Configuration.ORIENTATION_LANDSCAPE: 
      setContentView(layout_landscape) 
      break; 
     case Configuration.ORIENTATION_PORTRAIT: 
      setContentView(layout_portrait); 
      break; 
    } 
} 

無需調用setContentView(R.layout.videoview_main) and setupActivity()一次。

還需要在AdroidManifest中添加<activity android:name="<Your_video_avtivity>" android:configChanges="orientation" />

+0

我希望它最終能夠工作,但視頻在方向改變時再次重新下載 – Ugadaykin 2015-03-02 11:45:33

+0

它不應該,因爲使用' onConfigurationChanged' then activity does not destroy and restart。 – Xcihnegn 2015-03-02 11:49:03

+0

do you have'」android:configChanges =「orientation」/>'in the menifest?if not then add it – Xcihnegn 2015-03-02 12:13:37