2017-08-07 80 views
-1

我想在Android 5.1的全屏模式下播放VideoView的視頻。此外,我想在一段時間後停止視頻(並改變活動)(比如說5分鐘)。我怎樣才能做到這一點?我的佈局如下:在一段時間後停止Android視頻

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_height="fill_parent"  
    android:paddingLeft="2px" 
    android:paddingRight="2px" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:paddingTop="2px" 
    android:paddingBottom="2px" 
    android:layout_width="fill_parent" 
    android:orientation="vertical"> 

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

</LinearLayout> 

以下代碼:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.VideoView; 

public class VideoViewDemo extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     VideoView videoView = (VideoView)findViewById(R.id.VideoView); 

     videoView.setVideoPath("/sdcard/test.mp4"); 

     videoView.start(); 
    } 
} 
+0

的可能的複製[如何在Android的延遲之後調用一個方法] (https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android) – 0X0nosugar

回答

1

您應該使用「的MediaPlayer」,然後做一個「可運行」 5m處停止視頻。然後用處理你打電話給你的Runnable與」 .postDelayed)方法是這樣的:

public class VideoViewDemo extends Activity { 

     MediaPlayer mp; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 

      mp = MediaPlayer.create(this, "video"); 
      mp.start(); 

      Handler handler = new Handler(); 
      handler.postDelayed(stopPlayerTask, 500000); 
     } 

     Runnable stopPlayerTask = new Runnable(){ 
      @Override 
      public void run() { 
       mp.pause(); 
      }}; 
} 

你需要適應這個例子)