FLutter:未处理的异常:CameraException(INVALID_PATH,平台“TargetPlatform.android”

Nib*_*ain 6 video camera android video-recording flutter

我最近开始使用 flutter,在我的项目中我必须用计时器录制视频。当我单击停止按钮停止视频录制时,出现此错误。我尝试显示路径并得到了路径

I/flutter (19037): Video recorded to /data/user/0/app.package/cache/REC8929992591965700575.mp4


I/flutter (19037): Error: INVALID_PATH
I/flutter (19037): Error Message: The platform "TargetPlatform.android" did not return a path while reporting success. The platform should always return a valid path or report an error.

I/flutter (21723): Video recorded to /data/user/0/app.package/cache/REC4634449859696013797.mp4
E/flutter (21723): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: CameraException(INVALID_PATH, The platform "TargetPlatform.android" did not return a path while reporting success. The platform should always return a valid path or report an error.)
Run Code Online (Sandbox Code Playgroud)

使用这个插件:

  camera: ^0.9.4+5
  video_player: ^2.2.10
Run Code Online (Sandbox Code Playgroud)

我的代码:

 void _onStopButtonPressed() {
    if (cd.isRunning) { //timer
      cd.cancel();
    }
 _stopVideoRecording().then((file) {
      if (mounted) setState(() {});
      if (file != null) {
        videoPath = file; //getting the file
      }
     
    });
  }
Future<XFile> _stopVideoRecording() async {
    
    if (controller == null || !controller.value.isRecordingVideo) {
      return null;
    }

    try {
     return controller.stopVideoRecording();
    } on CameraException catch (e) {
      _showCameraException(e);
      return null;
    }
   }
Run Code Online (Sandbox Code Playgroud)

Sco*_*ion -1

我遇到了同样的情况并找到了解决方案。

尝试在停止或开始录制之后控制您的计时器控制器,而不是之前(否则您将收到错误)

像这样使用:

final file = await _controller?.stopVideoRecording();

  setState(() {
    _controller.reset();
    _isRecording = false;
  });
Run Code Online (Sandbox Code Playgroud)