How to record a video with Camera Plugin in flutter?

4,865

Solution 1

You are trying to save a video in your assets folder which is not possible ,

What you need to do is to save to device locally either common folders like downloads or app directory.

Here is an example of how to go about it

dependencies:
  path_provider:

Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.

We will be saving the video to app directory.

We need to get the path to the directory where the file is or will be. Usually a file is put in the application's document directory, in the application's cache directory, or in the external storage directory. To get the path easily and reduce the chance of type, we can use 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;
    
    }

Solution 2

In the new version, static method startRecordingVideo doesn't take any string parameter. When you want to start the recording just see whether a video is already getting recorded, if not start

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

and when you want to finish the recording you can call the static method stopVideoRecording() and it will give you a object of the class XFile, it will have the path to your video.

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

This thing has worked for me. I am new to flutter please improve my answer if you know more.

Share:
4,865
Flu_Py Developer
Author by

Flu_Py Developer

Updated on December 24, 2022

Comments

  • Flu_Py Developer
    Flu_Py Developer over 1 year

    I have this page where the camera is initialized and ready with a button that will record and stop the video, so I tried this :

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

    but throws this error : nhandled Exception: CameraException(videoRecordingFailed, assets/Videos/test.mp4: open failed: ENOENT (No such file or directory)). I don't understand, I don't want to open this file I want to save it there, Is there sth wrong with my code ?

  • Flu_Py Developer
    Flu_Py Developer over 3 years
    yes it worked, thanks. But is there a way to access this video in the emulator ?
  • griffins
    griffins over 3 years
    yes in files ,under your app name getApplicationDocumentsDirectory() Path to a directory where the application may place data that is user-generated, or that cannot otherwise be recreated by your application.
  • griffins
    griffins over 3 years
    replace getApplicationDocumentsDirectory() with getExternalStorageDirectory() // main storage folders path,but only works on android as IOS is not currently supported.
  • Eradicatore
    Eradicatore about 3 years
    This no longer compiles. The method controller.startVideoRecording(filePath) no longer takes a path input. Any idea where it goes now?
  • griffins
    griffins about 3 years
    they revamped the plugin, i will update the answer.check docs pub.dev/packages/camera/example @Eradicatore
  • Eradicatore
    Eradicatore about 3 years
    I just asked about this on github. Sounds like they are thinking of putting back the path as an optional parameter. github.com/flutter/flutter/issues/31225#issuecomment-7892243‌​39. @griffins