如何在播放4秒后暂停Android VideoView

时间:2014-06-18 08:38:51

标签: android time android-videoview mediacontroller postdelayed

我希望在播放4秒后暂停我的视频播放,然后我想向用户弹出一条消息。有人可以直接指出我如何在4秒后暂停视频。谢谢

V2 已实施@gogothebee反馈{现在视频暂停在第0位(实际开始之前)。

public class PlayVideoActivity extends ActionBarActivity {

    public static final String TAG = PlayVideoActivity.class.getSimpleName();
    Bundle bunde;
    Intent intent;
    String content_actual_content;

    VarController vc = new VarController(PlayVideoActivity.this);
    public ImageLoader imageLoader;

    private VideoView mVideoView;
    private MediaController mMediaController;
    ProgressBar pbplay1;

    private boolean isActive;
    private Handler handler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video); //playvideo (FullScreen)

        handler = new Handler();

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Set ProgressBar
        pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);

        //Get Link from ImageAdapter
        intent = this.getIntent();
        bunde = intent.getExtras(); 
        content_actual_content = bunde.getString("content_actual_content");

        Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Initiate Player
        mVideoView = (VideoView) findViewById(R.id.videoview);                      
        mVideoView.setVideoURI(Uri.parse(content_actual_content));
        mVideoView.setMediaController(new MediaController(PlayVideoActivity.this));
        mVideoView.requestFocus();

        mVideoView.setMediaController(mMediaController);

        //new myAsync().execute();

    }//--- end onCreate





    @Override
    protected void onPause() {
        super.onPause();
        isActive = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        isActive = true;
        mVideoView.requestFocus();
        mVideoView.start();
        scheduleVideoPause(4000);
    }

    private void scheduleVideoPause(int msec) {
        handler.removeCallbacksAndMessages(null);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!isActive) {
                    return;
                }
                mVideoView.pause();
                Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
            }
        }, msec);
    }

V1

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_video); //playvideo (FullScreen)

        //Hide Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        //Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Set ProgressBar
        pbplay1 = (ProgressBar) findViewById(R.id.pbplay1);

        //Get Link from ImageAdapter
        intent = this.getIntent();
        bunde = intent.getExtras(); 
        content_actual_content = bunde.getString("content_actual_content");

        Toast.makeText(PlayVideoActivity.this, "You're on Video Player with link: " + content_actual_content, Toast.LENGTH_SHORT).show();

        //Initiate Player
        mVideoView = (VideoView) findViewById(R.id.videoview);                      
        mVideoView.setVideoURI(Uri.parse(content_actual_content));
        mMediaController = new MediaController(PlayVideoActivity.this);
        mVideoView.setMediaController(mMediaController);
        mVideoView.requestFocus();

        mVideoView.start();
        //new myAsync().execute();


        Log.i("**********************", "getCurrentPosition: " + mVideoView.getCurrentPosition());

        if (mVideoView.getCurrentPosition() == 4000) {
            mVideoView.pause();
            Toast.makeText(PlayVideoActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
        }





    }//--- end onCreate

1 个答案:

答案 0 :(得分:2)

似乎没有办法轻易做到。但是,我的解决方案是生成一个每100毫秒运行一次的后台计时器(可调)并检查当前位置。一旦超过所需的阈值(在这种情况下为4000毫秒),视频就会停止。我知道它并不优雅,并且在4000毫秒时无法正常工作,但如果有人有更好的想法,请分享。调整池化间隔时,如果您希望尽可能接近4000毫秒,请不要因性能原因而过低。

这个解决方案也可以重构,因为它非常混乱,但我想用尽可能少的代码来展示这个概念。

private Timer timer;
private boolean isActive;
private VideoView mVideoView;
private final static int POOLING_INTERVAL_MS = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Initiate Player
    mVideoView = (VideoView) findViewById(R.id.videoview);
    mVideoView.setVideoURI(Uri.parse("Your-Video-URI"));
    mVideoView.setMediaController(new MediaController(this));
}

@Override
protected void onPause() {
    super.onPause();
    isActive = false;
    cancelProgressPooling();
}

@Override
protected void onResume() {
    super.onResume();
    isActive = true;
    mVideoView.requestFocus();
    mVideoView.start();
    initVideoProgressPooling(4000);
}

private void initVideoProgressPooling(final int stopAtMsec) {
    cancelProgressPooling();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            mVideoView.post(new Runnable() {
                @Override
                public void run() {
                    if (!isActive) {
                        cancelProgressPooling();
                        return;
                    }
                    if(mVideoView.getCurrentPosition() >= stopAtMsec) {
                        mVideoView.pause();
                        cancelProgressPooling();
                        Toast.makeText(MainActivity.this, "Video has PAUSED at: " + mVideoView.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }, 0, POOLING_INTERVAL_MS);
}

private void cancelProgressPooling() {
    if(timer != null) {
        timer.cancel();
    }
    timer = null;
}