youtube_explode_dart icon indicating copy to clipboard operation
youtube_explode_dart copied to clipboard

How do you pause and resume download?

Open JamLarana opened this issue 3 years ago • 2 comments

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?

JamLarana avatar Apr 10 '22 18:04 JamLarana

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.

Hexer10 avatar Apr 13 '22 21:04 Hexer10

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:

  1. get the file size File(path).statsSync().size
  2. create a new client with the header ( headers.addAll({"range": "byte=fileSize-}); read more about range header for more control

MSOB7YY avatar Aug 11 '23 11:08 MSOB7YY