Why doesn't my video play from cache in Flutter

39 views Asked by At

I am using Flutter to code my application. I am using Flutter_cache_manager to cache my videos. When i am caching my videos, there are saved as a .bin file in the cache. So, when i am passing my cached video file path to play it, it's not getting initialized. Though is works perfectly fine on Android, it doesn't work on IOS.

My caching class :

class video_caching_config {

  Future<String> initVideo(String url) async {
    try {
      final cacheManager = CacheManager(Config("video_cache", stalePeriod: const Duration(days: 2, hours: 6,), maxNrOfCacheObjects: 100, fileService: LongCacheHttpFileService(),),);
      final _file = await cacheManager.getFileFromCache("${url}");

      if(_file != null) {
        print("Exists");
        print("${_file.file.path}");
        return _file.file.path;
      } else {
        print("Doesn't Exist!");
        final _nFile = await CacheManager(Config("video_cache", stalePeriod: const Duration(days: 2, hours: 6,), maxNrOfCacheObjects: 100, fileService: LongCacheHttpFileService(),),).getSingleFile(url, key: "${url}");
        print("${_nFile.path}");
        return _nFile.path;
      }
    } catch (e) {
      print("Video Caching Config: ${e}");
      return "Error Occurred! Please Try Again!";
    }
  }
}

class LongCacheHttpFileService extends HttpFileService {
  final http.Client _httpClient;

  LongCacheHttpFileService({http.Client? httpClient})
      : _httpClient = httpClient ?? http.Client();

  @override
  Future<FileServiceResponse> get(String url, {Map<String, String>? headers}) async {
    final req = http.Request('GET', Uri.parse(url));
    req.headers.addAll(headers ?? {});

    final httpResponse = await _httpClient.send(req);
    final headersWithCacheControl = Map<String, String>.from(httpResponse.headers);
    headersWithCacheControl['cache-control'] = 'max-age=${const Duration(days: 10).inSeconds}';

    final responseWithUpdatedHeaders = http.StreamedResponse(
      httpResponse.stream,
      httpResponse.statusCode,
      headers: headersWithCacheControl,
      contentLength: httpResponse.contentLength,
      request: httpResponse.request,
    );

    return HttpGetResponse(responseWithUpdatedHeaders);
  }
}

Initializing Video

String videoPath = await video_caching_config().initVideo(_lVideoUrl);
_lVideoPath = videoPath;
var controller = VideoPlayerController.network(videoPath);
_videoController = controller;
if(mounted) {
  controller.initialize().then((_) {
    controller.addListener(_onControllerUpdate);
    if(mounted) {
      setState1(() {
        isVidInit = true;
      });
    }
  });
}

Can i know what I'm doing wrong here, or what i need to do to make it work on IOS. Or how can i save the the videos with custom extension?

0

There are 0 answers