Flutter Camera display recording video duration

Viewed 184

I would like to display video recording duration while continue video recording with flutter camera plugin. However plugin has not any event listener or another method to attach start and stop video record events. I can calculate duration when user pressed start record and stop record buttons but I look more precision solution.

1 Answers

Check this

UI Display camera with duration enter image description here

@override
  void initState() {
    super.initState();
    _stopwatch = Stopwatch();
  }

body: Expanded(
            child: Container(
              decoration: BoxDecoration(
                color: Colors.black,
                border: Border.all(
                  color:
                  controller != null && controller!.value.isRecordingVideo
                      ? Colors.redAccent
                      : Colors.grey,
                  width: 3.0,
                ),
              ),
              child: Padding(
                padding: const EdgeInsets.all(1.0),
                child: Stack(
                  children: [
                    Center(
                      child: _cameraPreviewWidget(),
                    ),
                    Align(
                        alignment: Alignment.bottomLeft,
                        child: Text(formatTime(_stopwatch.elapsedMilliseconds), style: TextStyle(fontSize: 48.0,color: Colors.red))),
                  ],
                ),
              ),
            ),
          ),

add timer function when the video started or Paused time

late Stopwatch _stopwatch;



void handleStartStop() {
    if (_stopwatch.isRunning) {
      _stopwatch.stop();
    } else {
      _stopwatch.start();
      // _stopwatch.reset();
    }
    setState(() {});    // re-render the page
  }

reset timer when video stoped call this function

void timerReset() {
    if (_stopwatch.isRunning) {
      setState(() {
        _stopwatch.reset();
      });  
    }
  }

// Start video

 void onVideoRecordButtonPressed() {
    startVideoRecording().then((value) {
      handleStartStop();
      if (mounted) {
        setState(() {
        });
      }
    });
  }

// Resume video

void onResumeButtonPressed() {
    resumeVideoRecording().then((_) {
     handleStartStop();
      if (mounted) {
        setState(() {});
      }
     showInSnackBar('Video recording resumed');
    });
  }

// stop video func

void onStopButtonPressed() {
    stopVideoRecording().then((XFile? file) {
      timerReset();
      if (mounted) {
        setState(() {});
      }
      if (file != null) {
        showInSnackBar('Video recorded to ${file.path}');
        videoFile = file;
        _startVideoPlayer();
      }
    });
  }
Related