【问题标题】:How to record a video with Camera Plugin in flutter?如何在 Flutter 中使用 Camera Plugin 录制视频?
【发布时间】:2020-09-25 19:04:47
【问题描述】:

我有这个页面,其中相机已初始化并准备好一个按钮,可以录制和停止视频,所以我尝试了这个:

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))。 我不明白,我不想打开这个文件我想把它保存在那里,我的代码有什么问题吗?

【问题讨论】:

    标签: flutter camera android-camera video-recording


    【解决方案1】:

    在新版本中,静态方法 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
      }
    

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

    【讨论】:

      【解决方案2】:

      您正在尝试将视频保存在您的资产文件夹中,这是不可能的,

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

      这里是一个如何去做的例子

      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;
          
          }
      

      【讨论】:

      • 是的,它有效,谢谢。但是有没有办法在模拟器中访问这个视频?
      • 是在文件中,在您的应用程序名称下 getApplicationDocumentsDirectory() 应用程序可以放置用户生成的数据或应用程序无法重新创建的数据的目录路径。
      • getExternalStorageDirectory() // main storage folders path,but only works on android as IOS is not currently supported. 替换getApplicationDocumentsDirectory()
      • 这不再编译。方法 controller.startVideoRecording(filePath) 不再需要路径输入。知道它现在去哪里了吗?
      • 我刚刚在 github 上问过这个问题。听起来他们正在考虑将路径作为可选参数放回原处。 github.com/flutter/flutter/issues/31225#issuecomment-789224339。 @格里芬
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-10
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      相关资源
      最近更新 更多