一段时间后停止播放Android视频

时间:2017-08-07 16:59:53

标签: android android-videoview android-video-player

我想在Android 5.1中以全屏模式播放VideoView的视频。此外,我想在一段时间后停止视频(并改变活动)(比方说5分钟)。我怎样才能做到这一点?我的布局如下:

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

1 个答案:

答案 0 :(得分:1)

您应该使用“MediaPlayer”然后制作“Runnable”以在5米处停止视频。然后使用处理程序,使用“.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();
            }};
}

您需要调整此示例;)