How can I limit video capturing via ImagePicker to 1 minutes maximum duration in Flutter?

Viewed 3787

I'm using ImagePicker to upload videos either from gallery or via capturing them from camera.

Problem is that I don't want the video to exceed 1 minute duration, when in gallery picking mode, I check the duration of selected video and show a message if video is longer than 1 minute.

How can I do something like retrica, open camera but with limit on video duration ?

3 Answers

use maxDuration provided by image_picker

 final PickedFile videoFile = await picker.getVideo( 
      source: ImageSource.camera,   
      maxDuration: const Duration(seconds: 60),
  );

I think you cant do this by ImagePicker because of this plugin capture video by phone default camera app and you haven't access to check and manage duration while capturing until the user stops capturing and return to your application

but if you use camera plugin you can do this because of this plugin capture video by your application and you have access to check video duration while user capture video

https://pub.dev/packages/camera

you can't controller it if you want to get this feature use Camera plugin https://pub.dev/packages/camera and use timer to stop recording

//Timer
    timer = Timer.periodic(Duration(seconds: 60), (Timer t) { 
            _onStopButtonPressed();
            timer.cancel();
          });
        });

//stop recording when click on the button
    void _onStopButtonPressed() {
        setState(() {
          buttonColor = Colors.white;
        });
            _stopVideoRecording().then((_) {
              if (mounted) setState(() {});


            });

        timer.cancel(); //when user close it manually
      }

// stop funcation
    Future<void> _stopVideoRecording() async {
        if (!controller.value.isRecordingVideo) {
          return null;
        }

    try {
      await controller.stopVideoRecording();
    } on CameraException catch (e) {
      _showCameraException(e);
      return null;
    }
  }

also you can use video_player plugin to replay the video after recording https://pub.dev/packages/video_player#-installing-tab-

Related