chewie_player.dart 8.3 KB

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