flutter - 使用 Flutter 中的图像选择器插件选取视频的最大持续时间

标签 flutter imagepicker

我正在使用图像选择器插件从我的 flutter 应用程序中的图库中挑选视频。我想将所选视频的最大持续时间设置为 30 秒。即使设置了最大持续时间,以下代码也不起作用。如果用户选择更大的视频,是否有任何方法可以显示错误或自动修剪前 30 秒。

pickVideo(ImageSource src) async {
Navigator.pop(context);
final video = await ImagePicker().getVideo(
  source: src,
  maxDuration: Duration(seconds: 30),
);

最佳答案

我通过在选择长度超过 x 秒的视频时抛出错误来解决此问题。它看起来如下:

 Future<void> pickVideo() async {

try {
  final picker = ImagePicker();
  var pickedFile = await picker.pickVideo(source: ImageSource.gallery, maxDuration: Duration(seconds: maxSeconds));
  if (pickedFile == null) {
    return;
  }
  VideoPlayerController testLengthController = new VideoPlayerController.file(File(pickedFile.path));//Your file here
  await testLengthController.initialize();
  if (testLengthController.value.duration.inSeconds > 60) {
    pickedFile = null;
    throw('we only allow videos that are shorter than 1 minute!');

  } else {
    setState(() {
      videoFile = XFile(pickedFile.path);
      _startVideoPlayer();
    });
  }
  testLengthController.dispose();
} catch (e) {
  showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          content: Container(
            child: Text(e.toString()),
          ),
        );
      });
  return;
}

}

关于flutter - 使用 Flutter 中的图像选择器插件选取视频的最大持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67243216/

相关文章:

dart - 使用 FutureBuilder 时如何根据另一个下拉列表选择更改下拉列表的项目?

flutter - 错误 : Unsupported operation: _Namespace when using image_picker flutter web

Flutter 的 web_socket_channel 静默失败

gradle - 从一台 PC 移动到另一台(window 到 mac)时出现 flutter 项目构建错误

flutter - Flutter 中的标准底板

flutter - 在另一个页面中过滤并显示结果

api - 如何使用 HTTP 将多张图片上传到 Flutter 中的 Rest API?

flutter - 如何在 flutter 中从图像选择器中裁剪图像?

android - Image_picker throws removeInvalidNode jank list 中的所有节点都超时而不是返回图像