2013-04-30 186 views
0

我想從一個網站在Android應用程序播放視頻。我有視頻播放,但有兩件事情我很擔心:如何停止視頻播放器後,它已經播放了Android的視頻

  1. 當我旋轉手機,而播放視頻時,它重新加載視頻,並再次從頭開始。

  2. 視頻結束後,它似乎仍然在做某些事情,我無法點擊後退按鈕並返回上一個活動。

的Java文件:

public class IntroVideo extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.intro_video); 
    ActionBar actionBar = getActionBar(); 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    try { 

     String link = "http://northeastvision.org/vids/tube_rush.mp4"; 
     final VideoView video = (VideoView) findViewById(R.id.videoDisplay); 
     final ProgressDialog mProgressDialog = new ProgressDialog(this); 
     mProgressDialog.setMessage("Loading video Please wait..."); 
     mProgressDialog.setIndeterminate(true); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 


     video.setMediaController(new MediaController(IntroVideo.this)); 
     Uri uri = Uri.parse(link); 
     video.setVideoURI(uri); 

     video.setOnPreparedListener(new OnPreparedListener() { 

      @Override 
      public void onPrepared(MediaPlayer mp) { 

       video.start(); 
       mProgressDialog.dismiss(); 
      } 
      }); 

    } 
    catch (Exception e) { 
     Toast.makeText(this, "Error Connecting!", Toast.LENGTH_SHORT).show(); 
    } 
    } 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

<VideoView 
     android:id="@+id/videoDisplay" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</RelativeLayout> 

回答

0

有一兩件事你可以做(​​這會爲你做兩件事:結束了與返回到前屏幕)將是:

video.setOnCompletionListener (new OnCompletionListener() { 

     @Override 
     public void onCompletion(MediaPlayer mp) { 

      mp.release(); 
      this.finish();//kills current Activity 
     } 
}); 
0

1)防止重載時旋轉手機添加屬性的android:configChanges =「方向|屏幕尺寸到您的Manifest.xml

<activity android:name="IntroVideo" 
       android:configChanges="orientation|screenSize" /> 

2)釋放與MediaPlayer的相關資源和完成活動。

video.setOnCompletionListener (new OnCompletionListener() { 
     @Override 
     public void onCompletion(MediaPlayer mp) { 
     video.release() 
     this.finish(); 
      } 
    }); 

瞭解更多關於:

android:configChangesMediaPLayer.release():