Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a count down timer that created to close video stream after 20 seconds.
My problem is the timer doesn't stop and counting very fast. So I have to call cancel. but when calling cancel it doesn't work.

please check my code below and let me know.
Thanks in advance

What I have tried:

Java
private void StartVideoTimer(int countdownPeriod){
Log.d(LOG_TAG, "[startVideoTimer] - Starting timer for video (in calibration), countdown period = " + countdownPeriod);
/**tried to cancel here*/
//        if (inCalibrationVideoTimer != null) {
//            inCalibrationVideoTimer.cancel();
//            inCalibrationVideoTimer = null;
//        }

streamIsRunning = true;

inCalibrationVideoTimer = new CountDownTimer(countdownPeriod, VIDEO_TIMER_INTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                final int secondsLeft = (int) millisUntilFinished / 1000;
                Log.d(LOG_TAG, "[onTick] - Seconds left = " + secondsLeft);

                SharedPreferences.Editor editor = getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE).edit();
                editor.putInt("inCalibrationvideoTimerCountdown", secondsLeft);
                editor.apply();

                try {
                    if (streamIsRunning) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
							videoTimeTextView.setText(String.format(getString(R.string.common_timer_format), secondsLeft));
                            }
                        });
                    }
                }
                catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinish() {
                try {
                    if (streamIsRunning) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                videoTimeTextView.setText(String.format(getString(R.string.common_timer_format), 20));
                            }
                        });
                    }
                }
                catch (IllegalStateException e) {
                    e.printStackTrace();
                }
            }
        }.start();
/**tried to cancel here but not working*/
    }
Posted
Comments
Richard MacCutchan 4-Apr-18 13:10pm    
What is the value of VIDEO_TIMER_INTERVAL?
Samira Radwan 4-Apr-18 13:24pm    
VIDEO_TIMER_INTERVAL = 1000
wseng 4-Apr-18 13:10pm    
where your cancel button ?
Samira Radwan 4-Apr-18 13:26pm    
There's no cancel button. My cancel code is in comment in the beginning of the method. Should timer have a cancel button?
Richard MacCutchan 5-Apr-18 3:23am    
I am still confused as to the actual problem you have. What do you mean by, "the timer doesn't stop and counting very fast."?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900