2017-08-07 103 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(); 
      }}; 
} 

你需要适应这个例子)

相关问题