Get the progress time of the video played under videoview?

后端 未结 4 555
温柔的废话
温柔的废话 2020-11-28 07:51

I need to get the progress time of the video that is played in \"VideoView\"(ie the time shown in the left hand side in progress bar).

Any help will really be apprec

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 08:03

    I accomplished it such way

    private void playVideo() {
        setViewNotVisibility();
    
        final String videoSource = "android.resource://" + getPackageName() + File.separator + R.raw.moviiegood;
        videoView.setKeepScreenOn(true);
        videoView.setVideoURI(Uri.parse(videoSource));
        videoView.setMediaController(null);
        videoView.setOnCompletionListener(myVideoViewCompletionListener);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                            @Override
                                            public void onPrepared(MediaPlayer mp) {
                                                startVideo();
                                                setDuration();
                                                timerCounter();
                                            }
                                        }
    
        );
    }
    
    private Timer timer;
    private void timerCounter(){
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        updateUI();
                    }
                });
            }
        };
        timer.schedule(task, 0, 1000);
    }
    
    private int duration = 0;
    
    private void setDuration(){
        duration = videoView.getDuration();
    }
    
    private void updateUI(){
        if (pbVideoView.getProgress() >= 100) {
            timer.cancel();
        }
        int current = videoView.getCurrentPosition();
        int progress = current * 100 / duration;
        pbVideoView.setProgress(progress);
    }
    

    If you have a questions feel free to ask

提交回复
热议问题