2017-08-07 11 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

の可能な複製を[アンドロイドの遅延の後にメソッドを呼び出す方法] (https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android) – 0X0nosugar

答えて

1

あなたが「MediaPlayerの」を使用して、5メートルでビデオを停止する「のRunnable」を行う必要があります私のレイアウトは次のようです。次に、ハンドラで、あなたは、このような「.postDelayed)メソッドを使用してRunnableを呼び出す:

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(); 
      }}; 
} 

あなたはこの例を適応させる必要があります。)

関連する問題