youtube_explode_dart
youtube_explode_dart copied to clipboard
How do you pause and resume download?
In situations where internet connection is lost, or when a user would like to pause downloading and resume downloading later. How do you handle that?
If it is fine for you to pause a download while still have the stream subscription open you can use the stream pause and resume methods:
final _sub = yt.videos.streams.get(stream).listen((data) {
// Process the video content.
}, onDone: () {
print('Download finished');
});
await Future.delayed(const Duration(seconds: 2));
_sub.pause();
print('Download paused for 5 seconds...');
await Future.delayed(const Duration(seconds: 5));
print('Resuming download');
_sub.resume();
if you want to pause a download and resume it for example after an application restart, then that's not currently possible.
you can achieve that by using range in http header
by pausing download, you are closing the request, and resuming means opening a new get request but with range header
so what you should do for resuming is:
- get the file size
File(path).statsSync().size - create a new client with the header
(
headers.addAll({"range": "byte=fileSize-});read more about range header for more control