我正在使用图像选择器插件从我的 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/