Flutter Camera显示录制视频时长

Dre*_*her 5 dart flutter

我想在使用颤动相机插件继续视频录制的同时显示视频录制持续时间。但是插件没有任何事件侦听器或其他方法来附加开始和停止视频记录事件。我可以计算用户按下开始记录和停止记录按钮时的持续时间,但我寻找更精确的解决方案。

Mah*_* Gv 3

检查这个

UI 显示相机的持续时间 在此输入图像描述

@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))),
                  ],
                ),
              ),
            ),
          ),
Run Code Online (Sandbox Code Playgroud)

添加视频开始或暂停时间时的计时器功能

late Stopwatch _stopwatch;



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

当视频停止时重置计时器调用此函数

void timerReset() {
    if (_stopwatch.isRunning) {
      setState(() {
        _stopwatch.reset();
      });  
    }
  }
Run Code Online (Sandbox Code Playgroud)

// 开始视频

 void onVideoRecordButtonPressed() {
    startVideoRecording().then((value) {
      handleStartStop();
      if (mounted) {
        setState(() {
        });
      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

// 恢复视频

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

// 停止视频功能

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