使用 Dio download 下载大型视频会导致内存不足
downloading a large video with Dio download causes out of memory
我正在尝试制作一个具有从 url 下载视频功能的 flutter 应用,并且视频预计会很大(至少 1 到 2 小时的 720p)。
我为此使用了 Dio,但它试图将完整的视频存储在 RAM 中,这会导致内存不足错误。这是我使用的代码。
有什么解决方法吗?或更好的解决方案?
Dio dio = Dio();
dio
.download(
url,
filePathAndName,
onReceiveProgress: (count, total) {
progressStream.add(count / total);
},
)
.asStream()
.listen((event) {
})
.onDone(() {
Fluttertoast.showToast(msg: "Video downloaded");
});
过了一会儿,它给出了这个异常:
java.lang.OutOfMemoryError
因为它是一个大文件,我认为最好使用 flutter_downloader 插件下载你的文件,它也支持通知和后台模式
初始化flutter下载器
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(
debug: true // optional: set false to disable printing logs to console
);
处理隔离
重要说明:您的 UI 在主隔离中呈现,而下载事件来自后台隔离(换句话说,回调中的代码在后台隔离中 运行),所以你必须处理两个隔离之间的通信。例如:
ReceivePort _port = ReceivePort();
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
_port.listen((dynamic data) {
String id = data[0];
DownloadTaskStatus status = data[1];
int progress = data[2];
setState((){ });
});
FlutterDownloader.registerCallback(downloadCallback);
}
@override
void dispose() {
IsolateNameServer.removePortNameMapping('downloader_send_port');
super.dispose();
}
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
send.send([id, status, progress]);
}
下载文件
final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);
我正在尝试制作一个具有从 url 下载视频功能的 flutter 应用,并且视频预计会很大(至少 1 到 2 小时的 720p)。
我为此使用了 Dio,但它试图将完整的视频存储在 RAM 中,这会导致内存不足错误。这是我使用的代码。
有什么解决方法吗?或更好的解决方案?
Dio dio = Dio();
dio
.download(
url,
filePathAndName,
onReceiveProgress: (count, total) {
progressStream.add(count / total);
},
)
.asStream()
.listen((event) {
})
.onDone(() {
Fluttertoast.showToast(msg: "Video downloaded");
});
过了一会儿,它给出了这个异常: java.lang.OutOfMemoryError
因为它是一个大文件,我认为最好使用 flutter_downloader 插件下载你的文件,它也支持通知和后台模式
初始化flutter下载器
WidgetsFlutterBinding.ensureInitialized();
await FlutterDownloader.initialize(
debug: true // optional: set false to disable printing logs to console
);
处理隔离
重要说明:您的 UI 在主隔离中呈现,而下载事件来自后台隔离(换句话说,回调中的代码在后台隔离中 运行),所以你必须处理两个隔离之间的通信。例如:
ReceivePort _port = ReceivePort();
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(_port.sendPort, 'downloader_send_port');
_port.listen((dynamic data) {
String id = data[0];
DownloadTaskStatus status = data[1];
int progress = data[2];
setState((){ });
});
FlutterDownloader.registerCallback(downloadCallback);
}
@override
void dispose() {
IsolateNameServer.removePortNameMapping('downloader_send_port');
super.dispose();
}
static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
final SendPort send = IsolateNameServer.lookupPortByName('downloader_send_port');
send.send([id, status, progress]);
}
下载文件
final taskId = await FlutterDownloader.enqueue(
url: 'your download link',
savedDir: 'the path of directory where you want to save downloaded files',
showNotification: true, // show download progress in status bar (for Android)
openFileFromNotification: true, // click on notification to open downloaded file (for Android)
);