The video_player plugin with the SUPER-POWER of caching using flutter_cache_manager.
Add the cached_video_player_plus
package to your pubspec.yaml
file:
dependencies:
cached_video_player_plus: ^3.0.3
Follow the installation instructions of video_player plugin.
Import the cached_video_player_plus
package into your Dart file:
import 'package:cached_video_player_plus/cached_video_player_plus.dart';
If you are already using the video_player plugin
- Use the
CachedVideoPlayerPlusController
class instead of theVideoPlayerController
. - Use the
CachedVideoPlayerPlus
class instead of theVideoPlayer
. - Use the
CachedVideoPlayerPlusValue
class instead of theVideoPlayerValue
.
If you are not using the video_player plugin
-
Create a
CachedVideoPlayerPlusController
instance and initialize it.final controller = CachedVideoPlayerPlusController.networkUrl( Uri.parse( 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', ), invalidateCacheIfOlderThan: const Duration(days: 69), )..initialize().then((value) async { controller.play(); setState(() {}); });
-
Pass the controller to the
CachedVideoPlayerPlus
widget.CachedVideoPlayerPlus(controller),
OR
return Scaffold( body: Center( child: controller.value.isInitialized ? AspectRatio( aspectRatio: controller.value.aspectRatio, child: CachedVideoPlayerPlus(controller), ) : const CircularProgressIndicator.adaptive(), ), );
-
Caching is only supported if the
CachedVideoPlayerPlusController
initialization method isnetwork()
ornetworkUrl()
.
The web platform does not support caching. So, the plugin will use the video_player plugin for the web platform.
However, to achieve caching on the web platform, you can use the workaround
of defining Cache-Control
headers in the httpHeaders
parameter of the
CachedVideoPlayerPlusController.network()
method.
final controller = CachedVideoPlayerPlusController.networkUrl(
Uri.parse(
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
),
httpHeaders: {
'Cache-Control': 'max-age=3600',
},
)..initialize().then((value) async {
controller.play();
setState(() {});
});
When the initialize()
method is called, the package checks if the video file
is cached or not. A video file is identified by its URL. If the video file
is not cached, then it is downloaded and cached. If the video file is cached,
then it is played from the cache.
If the cached video file is older than the specified
invalidateCacheIfOlderThan
parameter, then the cached video file is deleted
and a new video file is downloaded and cached.
When cache of a video is not found, the video will be played from the network and will be cached in the background to be played from the cache the next time.