Asynchronous 如何在Dart中一次处理列表2中的项目
我有一个视频目录,我想通过从Dart调用ffmpeg来压缩,但我想一次处理两个视频Asynchronous 如何在Dart中一次处理列表2中的项目,asynchronous,dart,async-await,Asynchronous,Dart,Async Await,我有一个视频目录,我想通过从Dart调用ffmpeg来压缩,但我想一次处理两个视频 import 'package:glob/glob.dart'; import 'dart:core'; void main(List<String> args) async { final videoFiles = Glob('E:/camera/*.mov'); var videos = videoFiles.listSync(); var videosInQueue = 0;
import 'package:glob/glob.dart';
import 'dart:core';
void main(List<String> args) async {
final videoFiles = Glob('E:/camera/*.mov');
var videos = videoFiles.listSync();
var videosInQueue = 0;
while (true) {
if (videos.isEmpty) {
print('No more videos to process');
break;
}
if (videosInQueue < 2){
print('\n$videosInQueue videos in the queue. Adding more...');
var video = videos.removeLast();
var videoInfo = await getVideoInfo(video);
if (videoInfo['shouldProcess']) {
videosInQueue += 1;
var response = ProcessVideo(videoInfo['path'], videoInfo['outputName']);
response.then((onValue) {
videosInQueue -= 1;
});
}
}else{
continue;
}
}
}
Future<int> ProcessVideo(String path, String outputName) async {
print('Processing $path');
await Future.delayed(const Duration(seconds: 5));
return 200;
}
当我运行这段代码时,它会将前两个视频添加到队列中,但它会卡在while循环中
输出:
0 videos in the queue. Adding more...
Processing E:/camera\recording0023-10-12-2019.mov
1 videos in the queue. Adding more...
Processing E:/camera\recording0024-10-12-2019.mov
我怀疑这个代码…:
response.then((onValue) {
videosInQueue -= 1;
});
被称为“视频队列”的次数不会减少。我怎样才能纠正这个问题
response.then((onValue) {
videosInQueue -= 1;
});