main.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import 'package:chewie/chewie.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:video_player/video_player.dart';
  5. void main() {
  6. runApp(
  7. MaterialApp(
  8. home: Page1(),
  9. )
  10. );
  11. }
  12. class Page1 extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. // TODO: implement build
  16. return Container(
  17. color: Colors.white,
  18. child: GestureDetector(
  19. onTap: () {
  20. Navigator.push<void>(context, CupertinoPageRoute(builder: (_) => ChewieDemo()));
  21. },
  22. )
  23. );
  24. }
  25. }
  26. class ChewieDemo extends StatefulWidget {
  27. ChewieDemo({this.title = 'Chewie Demo'});
  28. final String title;
  29. @override
  30. State<StatefulWidget> createState() {
  31. return _ChewieDemoState();
  32. }
  33. }
  34. class _ChewieDemoState extends State<ChewieDemo> {
  35. TargetPlatform _platform;
  36. VideoPlayerController _videoPlayerController1;
  37. VideoPlayerController _videoPlayerController2;
  38. ChewieController _chewieController;
  39. double aspectRatio=1;
  40. CartoonMaterialControls _i2materialControls;
  41. List<MoreVideo> lists = [];
  42. int currentVideoIndex=0;
  43. Future<void> _initializeVideoPlayerFuture;
  44. bool _disposed = false;
  45. var _isPlaying = false;
  46. var _isEndPlaying = false;
  47. bool _firstFullScreen = true;
  48. @override
  49. void initState() {
  50. super.initState();
  51. Future.delayed(Duration(milliseconds: 1000))
  52. .then((_){
  53. setState(() {
  54. lists.addAll([
  55. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "http://vod.cdn.i2edu.net/eschool/dubit/upload/cartoon/file/2019/12/10/0744228f-98bf-4255-8adc-c04c34b486d6.mp4", Duration(seconds: 1200), true),
  56. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
  57. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "http://vod.cdn.i2edu.net/eschool/dubit/upload/cartoon/file/2019/12/10/0744228f-98bf-4255-8adc-c04c34b486d6.mp4", Duration(seconds: 1200), false),
  58. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
  59. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "http://vod.cdn.i2edu.net/eschool/dubit/upload/cartoon/file/2019/12/10/0744228f-98bf-4255-8adc-c04c34b486d6.mp4", Duration(seconds: 1200), false),
  60. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
  61. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "http://vod.cdn.i2edu.net/eschool/dubit/upload/cartoon/file/2019/12/10/0744228f-98bf-4255-8adc-c04c34b486d6.mp4", Duration(seconds: 1200), false),
  62. MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
  63. ]);
  64. });
  65. _i2materialControls = CartoonMaterialControls(
  66. enableQuickControl: true,
  67. moreVideo: lists,
  68. onTapMoreVideo: (index) {
  69. print("onTapMoreVideo $index");
  70. _startPlay(index);
  71. },
  72. onTapPrevious: () {
  73. if (currentVideoIndex - 1 < 0) return;
  74. _startPlay(currentVideoIndex - 1);
  75. },
  76. onTapNext: () {
  77. if (currentVideoIndex + 1 >= lists.length) return;
  78. _startPlay(currentVideoIndex + 1);
  79. },
  80. onTapLove: () {
  81. },
  82. );
  83. _initializePlay(0);
  84. });
  85. _videoPlayerController2 = VideoPlayerController.network(
  86. 'https://v-cdn.zjol.com.cn/276985.mp4')
  87. ..initialize().then((_){
  88. setState(() {
  89. });
  90. });
  91. }
  92. @override
  93. void dispose() {
  94. _disposed = true;
  95. _videoPlayerController1.dispose();
  96. _videoPlayerController2.dispose();
  97. _chewieController.dispose();
  98. super.dispose();
  99. }
  100. void delayEnterFullScreen() async {
  101. Future.delayed(Duration(milliseconds: 100))
  102. .then((_){
  103. _chewieController.toggleFullScreen();
  104. });
  105. }
  106. @override
  107. Widget build(BuildContext context) {
  108. return MaterialApp(
  109. title: widget.title,
  110. theme: ThemeData.light().copyWith(
  111. platform: _platform ?? Theme.of(context).platform,
  112. ),
  113. home: Scaffold(
  114. backgroundColor: Colors.black,
  115. body: Column(
  116. children: <Widget>[
  117. Stack(
  118. children: <Widget>[
  119. Container(
  120. // constraints: BoxConstraints(maxHeight: 200),
  121. child: _playView(),
  122. color: Colors.black,
  123. ),
  124. ],
  125. )
  126. ],
  127. ),
  128. ),
  129. );
  130. }
  131. Future<bool> _clearPrevious() async {
  132. await _videoPlayerController1?.pause();
  133. _videoPlayerController1?.removeListener(_controllerListener);
  134. return true;
  135. }
  136. // tracking status
  137. void _controllerListener() {
  138. if (_videoPlayerController1 == null || _disposed) {
  139. return;
  140. }
  141. if (!_videoPlayerController1.value.initialized) {
  142. return;
  143. }
  144. final position = _videoPlayerController1.value.position;
  145. final duration = _videoPlayerController1.value.duration;
  146. if (duration != null && position != null) {
  147. final isPlaying = position.inMilliseconds < duration.inMilliseconds;
  148. final isEndPlaying = position.inMilliseconds > 0 &&
  149. position.inSeconds == duration.inSeconds;
  150. if (_isPlaying != isPlaying || _isEndPlaying != isEndPlaying) {
  151. _isPlaying = isPlaying;
  152. _isEndPlaying = isEndPlaying;
  153. print(
  154. "$currentVideoIndex -----> isPlaying=$isPlaying / isCompletePlaying=$isEndPlaying");
  155. if (isEndPlaying) {
  156. final isComplete = currentVideoIndex == lists.length - 1;
  157. if (isComplete) {
  158. print("played all!!");
  159. } else {
  160. _startPlay(currentVideoIndex + 1);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. Future<void> _startPlay(int index) async {
  167. print("play ---------> $index");
  168. setState(() {
  169. _initializeVideoPlayerFuture = null;
  170. });
  171. Future.delayed(const Duration(milliseconds: 200), () {
  172. _clearPrevious().then((_){
  173. _initializePlay(index);
  174. });
  175. });
  176. }
  177. Future<void> _initializePlay(int index) async {
  178. _videoPlayerController1 = VideoPlayerController.network(lists[index].videoUrl);
  179. _videoPlayerController1.addListener(_controllerListener);
  180. _chewieController = ChewieController(
  181. videoPlayerController: _videoPlayerController1,
  182. customControls: _i2materialControls,
  183. fullScreenByDefault: true,
  184. forceFullScreen: true,
  185. allowedScreenSleep: false,
  186. showControls: true,
  187. // showControlsOnInitialize: true,
  188. );
  189. _chewieController?.addListener((){
  190. if (_videoPlayerController1 != null && !_chewieController.isFullScreen) {
  191. Navigator.of(context).maybePop();
  192. }
  193. });
  194. _initializeVideoPlayerFuture = _videoPlayerController1.initialize();
  195. setState(() {
  196. currentVideoIndex = index;
  197. for (int i = 0; i < lists.length; i++) {
  198. lists[i].isSelect = (i == index);
  199. }
  200. });
  201. }
  202. // play view area
  203. Widget _playView() {
  204. // FutureBuilder to display a loading spinner until finishes initializing
  205. return FutureBuilder(
  206. future: _initializeVideoPlayerFuture,
  207. builder: (BuildContext context, AsyncSnapshot snapshot) {
  208. if (snapshot.connectionState == ConnectionState.done) {
  209. _chewieController.play();
  210. return AspectRatio(
  211. aspectRatio: _videoPlayerController1.value.aspectRatio,
  212. child: Chewie(controller: _chewieController),
  213. );
  214. } else {
  215. return SizedBox(
  216. height: MediaQuery.of(context).size.height,
  217. child: Center(child: CircularProgressIndicator()),
  218. );
  219. }
  220. },
  221. );
  222. }
  223. }