Flutter-流和缓存视频 [英] Flutter - Streaming and Caching videos

查看:438
本文介绍了Flutter-流和缓存视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Flutter应用程序,该应用程序在列表中显示视频(例如Instagram).必须流式传输视频,这样我才能先下载视频然后再播放.

I'm developing an application in flutter which is showing videos in a list (like Instagram). Videos must be streamed so I can't download them first and then play them.

我想在流式传输时缓存它们.我见过 CacheManager 类,但是它将下载整个文件,然后将其传递给视频播放器来播放.

I want to cache them while they are being streamed. I've seen CacheManager class but it will download the whole file and then pass it to video player to play it.

在将视频文件下载到缓存文件夹时,如何实现缓存管理器以显示视频文件?

How can I implement a cache manager to show the video file while it is being downloaded to cache folder?

推荐答案

我可能写得有点晚,但是以防万一很快有人在寻找解决方案.目前,官方的video_player插件尚不支持通过网络进行视频缓存.

I might be writing this a bit late but just in case anybody looking for a solution soon. By the moment the official video_player plugin doesn't support video caching over network yet.

但幸运的是,贡献者曾尝试过将缓存功能添加到video_player插件中您可以在此处跟踪更新并找到其他PR: https://github.com/flutter/flutter/issues/28094

But fortunately, there been some attempts by contributors to add caching feature to the video_player plugin You can track updates and find another PRs here: https://github.com/flutter/flutter/issues/28094

  video_player:
    git:
      url: https://github.com/sanekyy/plugins.git
      ref: caching
      path: packages/video_player/video_player   

如果您使用的是chewie或其他依赖video_player插件的库,请添加:

In case of you are using chewie or other libraries that depend on video_player plugin add:

dependency_overrides:
  video_player:
    git:
      url: https://github.com/sanekyy/plugins.git
      ref: caching
      path: packages/video_player/video_player

现在将视频传递 useCache:true 缓存到

_videoPlayerController = VideoPlayerController.network(videoURL, useCache: true);

默认情况下,maxFileSize和maxCacheSize都设置为10 * 1024 * 1024字节.要调整缓存大小,请执行以下操作:

By default both maxFileSize and maxCacheSize is set to 10 * 1024 * 1024 bytes. To adjust the cache size:

  VideoPlayerController.setCacheSize(100 * 1024 * 1024, 200 * 1024 * 1024);


另一种解决方案:是要使用官方插件正常流式传输视频,并同时使用 flutter_cache_manager 来缓存视频文件.


Another Solution: Is to stream the video normally using the official plugin and to cache the video file using flutter_cache_manager simultaneously.

但这将导致第一次获取视频两次(一次是通过video_player流式传输,另一次是通过cachemanager下载视频)

But this will lead to fetch the video twice the first time (Once for streaming through the video_player, Another for downloading the video through the cachemanager)

这是该场景的运行方式:

Here how the scenario would goes:

1-使用flutter_cache_manager检查是否已下载并缓存了video_url.

1- Check with flutter_cache_manager if the video_url is already downloaded and cached.

2-如果视频已缓存,则将文件路径传递给video_player VideoPlayerController.file(path),如果未使用cachemanager下载文件,并使用 VideoPlayerController.network(videoURL)(目前,视频播放器和cachemanager已将视频抓取了两次...).

2- if the video is cached, pass the file path to video_player VideoPlayerController.file(path), if not download the file using cachemanager and stream the video using VideoPlayerController.network(videoURL) (at this moment video is being fetched twice... by videoplayer and cachemanager).

这篇关于Flutter-流和缓存视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆