chewie_player.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import 'dart:async';
  2. import 'package:chewie/src/chewie_progress_colors.dart';
  3. import 'package:chewie/src/player_with_controls.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter/widgets.dart';
  7. import 'package:screen/screen.dart';
  8. import 'package:video_player/video_player.dart';
  9. typedef Widget ChewieRoutePageBuilder(
  10. BuildContext context,
  11. Animation<double> animation,
  12. Animation<double> secondaryAnimation,
  13. _ChewieControllerProvider controllerProvider);
  14. /// A Video Player with Material and Cupertino skins.
  15. ///
  16. /// `video_player` is pretty low level. Chewie wraps it in a friendly skin to
  17. /// make it easy to use!
  18. class Chewie extends StatefulWidget {
  19. Chewie({
  20. Key key,
  21. this.controller,
  22. }) : assert(controller != null, 'You must provide a chewie controller'),
  23. super(key: key);
  24. /// The [ChewieController]
  25. final ChewieController controller;
  26. @override
  27. ChewieState createState() {
  28. return ChewieState();
  29. }
  30. }
  31. class ChewieState extends State<Chewie> {
  32. bool _isFullScreen = false;
  33. @override
  34. void initState() {
  35. super.initState();
  36. widget.controller.addListener(listener);
  37. }
  38. @override
  39. void dispose() {
  40. widget.controller.removeListener(listener);
  41. super.dispose();
  42. }
  43. @override
  44. void didUpdateWidget(Chewie oldWidget) {
  45. if (oldWidget.controller != widget.controller) {
  46. widget.controller.addListener(listener);
  47. }
  48. super.didUpdateWidget(oldWidget);
  49. }
  50. void listener() async {
  51. if (widget.controller.isFullScreen && !_isFullScreen) {
  52. _isFullScreen = true;
  53. await _pushFullScreenWidget(context);
  54. } else if (_isFullScreen) {
  55. Navigator.of(context).pop();
  56. _isFullScreen = false;
  57. }
  58. }
  59. @override
  60. Widget build(BuildContext context) {
  61. return _ChewieControllerProvider(
  62. controller: widget.controller,
  63. child: PlayerWithControls(),
  64. );
  65. }
  66. Widget _buildFullScreenVideo(
  67. BuildContext context,
  68. Animation<double> animation,
  69. _ChewieControllerProvider controllerProvider) {
  70. return Scaffold(
  71. resizeToAvoidBottomPadding: false,
  72. body: Container(
  73. alignment: Alignment.center,
  74. color: Colors.black,
  75. child: controllerProvider,
  76. ),
  77. );
  78. }
  79. AnimatedWidget _defaultRoutePageBuilder(
  80. BuildContext context,
  81. Animation<double> animation,
  82. Animation<double> secondaryAnimation,
  83. _ChewieControllerProvider controllerProvider) {
  84. return AnimatedBuilder(
  85. animation: animation,
  86. builder: (BuildContext context, Widget child) {
  87. return _buildFullScreenVideo(context, animation, controllerProvider);
  88. },
  89. );
  90. }
  91. Widget _fullScreenRoutePageBuilder(
  92. BuildContext context,
  93. Animation<double> animation,
  94. Animation<double> secondaryAnimation,
  95. ) {
  96. var controllerProvider = _ChewieControllerProvider(
  97. controller: widget.controller,
  98. child: PlayerWithControls(),
  99. );
  100. if (widget.controller.routePageBuilder == null) {
  101. return _defaultRoutePageBuilder(
  102. context, animation, secondaryAnimation, controllerProvider);
  103. }
  104. return widget.controller.routePageBuilder(
  105. context, animation, secondaryAnimation, controllerProvider);
  106. }
  107. Future<dynamic> _pushFullScreenWidget(BuildContext context) async {
  108. final isAndroid = Theme.of(context).platform == TargetPlatform.android;
  109. final TransitionRoute<Null> route = PageRouteBuilder<Null>(
  110. settings: RouteSettings(isInitialRoute: false),
  111. pageBuilder: _fullScreenRoutePageBuilder,
  112. );
  113. SystemChrome.setEnabledSystemUIOverlays([]);
  114. if (isAndroid) {
  115. SystemChrome.setPreferredOrientations([
  116. DeviceOrientation.landscapeLeft,
  117. DeviceOrientation.landscapeRight,
  118. ]);
  119. }
  120. if (!widget.controller.allowedScreenSleep) {
  121. Screen.keepOn(true);
  122. }
  123. await Navigator.of(context).push(route);
  124. _isFullScreen = false;
  125. widget.controller.exitFullScreen();
  126. bool isKeptOn = await Screen.isKeptOn;
  127. if (isKeptOn) {
  128. Screen.keepOn(false);
  129. }
  130. SystemChrome.setEnabledSystemUIOverlays(
  131. widget.controller.systemOverlaysAfterFullScreen);
  132. SystemChrome.setPreferredOrientations(
  133. widget.controller.deviceOrientationsAfterFullScreen);
  134. }
  135. }
  136. /// The ChewieController is used to configure and drive the Chewie Player
  137. /// Widgets. It provides methods to control playback, such as [pause] and
  138. /// [play], as well as methods that control the visual appearance of the player,
  139. /// such as [enterFullScreen] or [exitFullScreen].
  140. ///
  141. /// In addition, you can listen to the ChewieController for presentational
  142. /// changes, such as entering and exiting full screen mode. To listen for
  143. /// changes to the playback, such as a change to the seek position of the
  144. /// player, please use the standard information provided by the
  145. /// `VideoPlayerController`.
  146. class ChewieController extends ChangeNotifier {
  147. ChewieController({
  148. this.videoPlayerController,
  149. this.aspectRatio,
  150. this.autoInitialize = false,
  151. this.autoPlay = false,
  152. this.startAt,
  153. this.looping = false,
  154. this.fullScreenByDefault = false,
  155. this.cupertinoProgressColors,
  156. this.materialProgressColors,
  157. this.placeholder,
  158. this.overlay,
  159. this.showControlsOnInitialize = true,
  160. this.showControls = true,
  161. this.customControls,
  162. this.errorBuilder,
  163. this.allowedScreenSleep = true,
  164. this.isLive = false,
  165. this.allowFullScreen = true,
  166. this.allowMuting = true,
  167. this.systemOverlaysAfterFullScreen = SystemUiOverlay.values,
  168. this.deviceOrientationsAfterFullScreen = const [
  169. DeviceOrientation.portraitUp,
  170. DeviceOrientation.portraitDown,
  171. DeviceOrientation.landscapeLeft,
  172. DeviceOrientation.landscapeRight,
  173. ],
  174. this.routePageBuilder = null,
  175. }) : assert(videoPlayerController != null,
  176. 'You must provide a controller to play a video') {
  177. _initialize();
  178. }
  179. /// The controller for the video you want to play
  180. final VideoPlayerController videoPlayerController;
  181. /// Initialize the Video on Startup. This will prep the video for playback.
  182. final bool autoInitialize;
  183. /// Play the video as soon as it's displayed
  184. final bool autoPlay;
  185. /// Start video at a certain position
  186. final Duration startAt;
  187. /// Whether or not the video should loop
  188. final bool looping;
  189. /// Weather or not to show the controls when initializing the widget.
  190. final bool showControlsOnInitialize;
  191. /// Whether or not to show the controls at all
  192. final bool showControls;
  193. /// Defines customised controls. Check [MaterialControls] or
  194. /// [CupertinoControls] for reference.
  195. final Widget customControls;
  196. /// When the video playback runs into an error, you can build a custom
  197. /// error message.
  198. final Widget Function(BuildContext context, String errorMessage) errorBuilder;
  199. /// The Aspect Ratio of the Video. Important to get the correct size of the
  200. /// video!
  201. ///
  202. /// Will fallback to fitting within the space allowed.
  203. final double aspectRatio;
  204. /// The colors to use for controls on iOS. By default, the iOS player uses
  205. /// colors sampled from the original iOS 11 designs.
  206. final ChewieProgressColors cupertinoProgressColors;
  207. /// The colors to use for the Material Progress Bar. By default, the Material
  208. /// player uses the colors from your Theme.
  209. final ChewieProgressColors materialProgressColors;
  210. /// The placeholder is displayed underneath the Video before it is initialized
  211. /// or played.
  212. final Widget placeholder;
  213. /// A widget which is placed between the video and the controls
  214. final Widget overlay;
  215. /// Defines if the player will start in fullscreen when play is pressed
  216. final bool fullScreenByDefault;
  217. /// Defines if the player will sleep in fullscreen or not
  218. final bool allowedScreenSleep;
  219. /// Defines if the controls should be for live stream video
  220. final bool isLive;
  221. /// Defines if the fullscreen control should be shown
  222. final bool allowFullScreen;
  223. /// Defines if the mute control should be shown
  224. final bool allowMuting;
  225. /// Defines the system overlays visible after exiting fullscreen
  226. final List<SystemUiOverlay> systemOverlaysAfterFullScreen;
  227. /// Defines the set of allowed device orientations after exiting fullscreen
  228. final List<DeviceOrientation> deviceOrientationsAfterFullScreen;
  229. /// Defines a custom RoutePageBuilder for the fullscreen
  230. final ChewieRoutePageBuilder routePageBuilder;
  231. static ChewieController of(BuildContext context) {
  232. final chewieControllerProvider =
  233. context.inheritFromWidgetOfExactType(_ChewieControllerProvider)
  234. as _ChewieControllerProvider;
  235. return chewieControllerProvider.controller;
  236. }
  237. bool _isFullScreen = false;
  238. bool get isFullScreen => _isFullScreen;
  239. Future _initialize() async {
  240. await videoPlayerController.setLooping(looping);
  241. if ((autoInitialize || autoPlay) &&
  242. !videoPlayerController.value.initialized) {
  243. await videoPlayerController.initialize();
  244. }
  245. if (autoPlay) {
  246. if (fullScreenByDefault) {
  247. enterFullScreen();
  248. }
  249. await videoPlayerController.play();
  250. }
  251. if (startAt != null) {
  252. await videoPlayerController.seekTo(startAt);
  253. }
  254. if (fullScreenByDefault) {
  255. videoPlayerController.addListener(() async {
  256. if (videoPlayerController.value.isPlaying && !_isFullScreen) {
  257. enterFullScreen();
  258. }
  259. });
  260. }
  261. }
  262. void enterFullScreen() {
  263. _isFullScreen = true;
  264. notifyListeners();
  265. }
  266. void exitFullScreen() {
  267. _isFullScreen = false;
  268. notifyListeners();
  269. }
  270. void toggleFullScreen() {
  271. _isFullScreen = !_isFullScreen;
  272. notifyListeners();
  273. }
  274. Future<void> play() async {
  275. await videoPlayerController.play();
  276. }
  277. Future<void> setLooping(bool looping) async {
  278. await videoPlayerController.setLooping(looping);
  279. }
  280. Future<void> pause() async {
  281. await videoPlayerController.pause();
  282. }
  283. Future<void> seekTo(Duration moment) async {
  284. await videoPlayerController.seekTo(moment);
  285. }
  286. Future<void> setVolume(double volume) async {
  287. await videoPlayerController.setVolume(volume);
  288. }
  289. }
  290. class _ChewieControllerProvider extends InheritedWidget {
  291. const _ChewieControllerProvider({
  292. Key key,
  293. @required this.controller,
  294. @required Widget child,
  295. }) : assert(controller != null),
  296. assert(child != null),
  297. super(key: key, child: child);
  298. final ChewieController controller;
  299. @override
  300. bool updateShouldNotify(_ChewieControllerProvider old) =>
  301. controller != old.controller;
  302. }