vikram25897 / flutter_cached_video_player

BSD 3-Clause "New" or "Revised" License
95 stars 145 forks source link

Preload next video #11

Open falguni0314 opened 4 years ago

falguni0314 commented 4 years ago

Hi, thanks for a great player, I have below query

Is there a way to preload next videos.

hyobbb commented 3 years ago

I am using flutter cache manager(https://pub.dev/packages/flutter_cache_manager). you can listen to your CachedVideoPlayerController and depends on the position of your controller you may start caching next source. for example:

    CachedVideoPlayerController controller;
    controller = CachedVideoPlayerController.network(source)..addListener(() { 
      if(controller.value.position > controller.value.duration / 2){
        _cacheManager.getSingleFile(nextSource);
      }
    });
HappySinha commented 2 years ago

I am using flutter cache manager(https://pub.dev/packages/flutter_cache_manager). you can listen to your CachedVideoPlayerController and depends on the position of your controller you may start caching next source. for example:

    CachedVideoPlayerController controller;
    controller = CachedVideoPlayerController.network(source)..addListener(() { 
      if(controller.value.position > controller.value.duration / 2){
        _cacheManager.getSingleFile(nextSource);
      }
    });

I'm using this code to preload the next video this is working smoothly so you can use the same code

`cacheNextVideos() {
   const int maxCache = 10, batch = 2;
   int rem = widget.snapshots.length - (widget.current + 1);
   int avail = rem > maxCache ? maxCache : rem;

 List<String> videosUrls = widget.snapshots
     .sublist(widget.current + 1, widget.current + avail + 1)
     .map<String>((v) => v['videoUrl'])
     .where((u) => u != null && u != "")
     .toList();

 List<List<String>> batchUrls = [];

 for (int i = 0; i < (videosUrls.length / batch); i++) {
   batchUrls.add(videosUrls.sublist((i * batch), ((i + 1) * batch)));
 }

 Future.forEach(batchUrls, (List<String> b) async {
   cachedCtrls =
       b.map((u) => CachedVideoPlayerController.network(u)).toList();

   await Future.wait(cachedCtrls.map((ctrl) async {
     await ctrl.initialize();
     ctrl?.dispose();
     cachedCtrls.removeWhere((c) => c == ctrl);
     return 0;
   }));
 });
}`