如何用相机插件在颤动中录制视频?

问题描述

我在此页面上初始化了摄像机,并准备好一个用于记录和停止视频的按钮,因此我尝试了此操作:

FlatButton(
     onpressed: () => {
            !isRecording
                ? {
                   setState(() {
                   isRecording = true;
                  }),cameraController.prepareForVideoRecording(),cameraController.startVideoRecording('assets/Videos/test.mp4')
                }
               : cameraController.stopVideoRecording(),},............

但抛出此错误nhandled Exception: CameraException(videoRecordingFailed,assets/Videos/test.mp4: open Failed: ENOENT (No such file or directory))。 我不明白,我不想打开这个文件,我想保存在那里,我的代码有问题吗?

解决方法

您正在尝试将视频保存到资产文件夹中,

您需要做的是将常见文件夹(例如下载文件夹或应用程序目录)保存到本地设备。

这是一个如何做的例子

dependencies:
  path_provider:

Flutter插件,用于获取主机平台上的常用位置 文件系统,例如temp和app数据目录。

我们将视频保存到应用目录。

我们需要获取文件所在目录或将要存放目录的路径。通常,文件放置在应用程序的文档目录,应用程序的缓存目录或外部存储目录中。为了轻松获取路径并减少输入的机会,我们可以使用PathProvider

 Future<String> _startVideoRecording() async {
    
      if (!controller.value.isInitialized) {      
    
        return null;
    
      }  
    
      // Do nothing if a recording is on progress
    
      if (controller.value.isRecordingVideo) {
    
        return null;
    
      }
  //get storage path
    
      final Directory appDirectory = await getApplicationDocumentsDirectory();
    
      final String videoDirectory = '${appDirectory.path}/Videos';
    
      await Directory(videoDirectory).create(recursive: true);
    
      final String currentTime = DateTime.now().millisecondsSinceEpoch.toString();
    
      final String filePath = '$videoDirectory/${currentTime}.mp4';
    
  
    
      try {
    
        await controller.startVideoRecording(filePath);
    
        videoPath = filePath;
    
      } on CameraException catch (e) {
    
        _showCameraException(e);
    
        return null;
    
      }
    
  
    //gives you path of where the video was stored
      return filePath;
    
    }
,

在新版本中,静态方法 startRecordingVideo 不带任何字符串参数。 当您要开始录制时,只需查看视频是否已被录制,如果未开始

  if (!_controller.value.isRecordingVideo) {
        _controller.startVideoRecording(); 
  }

当你想完成录制时,你可以调用静态方法 stopVideoRecording() ,它会给你一个 XFile 类的对象,它会有你的视频的路径。

  if (_controller.value.isRecordingVideo) {
      XFile videoFile = await _controller.stopVideoRecording();
      print(videoFile.path);//and there is more in this XFile object
  }

这件事对我有用。我是新手,如果你知道更多,请改进我的答案。