首页 > 解决方案 > 更新时间时进度条停止或滞后

问题描述

我在更新进度条时遇到了一个小问题。当用户按住录制按钮时,它会触发一个 CountDownTimer 任务,该任务会倒计时有多少秒的录制时间,并每 1000 毫秒增加已录制的秒数。当用户松开按钮时,它会取消 CountDownTimer 并在用户再次按下按钮后重新启动。有时当我按住记录按钮时,进度条会停止更新或滞后,即使我可以清楚地看到秒数仍在计数(我让它们在控制台上打印出来)。谁能帮我理解为什么它有时会滞后或停止?

这是任务:

countDownTimer = new CountDownTimer(millisLeftForRecording,1000) {
        @Override
        public void onTick(long l) {
            millisLeftForRecording = l;
            secondsRecorded++;
            lastNumberOfSecondsRecorded++;
            millisRecorded = secondsRecorded * 1000;

            System.out.println("secondsRecorded "+ secondsRecorded);
        }

        @Override
        public void onFinish() {
            //Do something
        }
    };

这是触发录制的按钮

recordButton.setOnTouchListener((view, motionEvent) -> {

        if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
            stopRecording();
        } else {
            startRecording();
        }
        return true;

    });

这是 startRecording 和 stopRecording 方法

private void startRecording(){

    progressBarFill(countDownTimer,true, isRecording);

    isRecording = true;
    recordButton.setVisibility(View.INVISIBLE);
    recordingImage.setVisibility(View.VISIBLE);

    if(secondsRecorded >= 10){
        saveVideoButton.setVisibility(View.VISIBLE);
    } else {
        saveVideoButton.setVisibility(View.INVISIBLE);
    }

}

// Stop capturing video
private void stopRecording(){

    progressBarFill(countDownTimer,false, isRecording);

    isRecording = false;

    videoLengthList.add(lastNumberOfSecondsRecorded);

    System.out.println("Last number of seconds recorded: " + lastNumberOfSecondsRecorded);

    lastNumberOfSecondsRecorded = 0;

    System.out.println("Video length list size: " + videoLengthList.size());

    videoSegmentsRecorded++;
    System.out.println("\n\nVideo Segments Recorded " + videoSegmentsRecorded +"\n\n");

    recordButton.setImageResource(R.drawable.record_button_unpressed_98);
    recordButton.setVisibility(View.VISIBLE);
    recordingImage.setVisibility(View.INVISIBLE);

    if(videoSegmentsRecorded > 0){
        deleteSegmentButton.setVisibility(View.VISIBLE);
    }
}

虽然这会更新时间,但我有一种方法可以使用已过去的新秒数更新进度条。这是那个方法:

private void progressBarFill(CountDownTimer aCountDownTimer, boolean fillProgressBar, boolean alreadyRunning){

    if(fillProgressBar && !alreadyRunning) {
        aCountDownTimer.start();
        System.out.println("\n\n\nCOUNT DOWN TIMER STARTED:\nAT " + secondsRecorded + " SECONDS\n\n\n");
    } else if(!fillProgressBar && alreadyRunning){
        aCountDownTimer.cancel();
        System.out.println("\n\n\nCOUNT DOWN TIMER STOPPED:\nAT " + secondsRecorded + " SECONDS\n\n\n");
    }
    \\secondsRecorded is multiplied by 1000000 because the progress bar max is 60 multiplied by 1000000

    videoLengthProgressBar.setProgress(secondsRecorded*1000000, true);

}

预先感谢您的帮助。

标签: javaandroidandroid-progressbarcountdowntimer

解决方案


推荐阅读