123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import 'package:chewie/chewie.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:video_player/video_player.dart';
- void main() {
- runApp(
- MaterialApp(
- home: Page1(),
- )
- );
- }
- class Page1 extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- // TODO: implement build
- return Container(
- color: Colors.white,
- child: GestureDetector(
- onTap: () {
- Navigator.push<void>(context, CupertinoPageRoute(builder: (_) => ChewieDemo()));
- },
- )
- );
- }
- }
- class ChewieDemo extends StatefulWidget {
- ChewieDemo({this.title = 'Chewie Demo'});
- final String title;
- @override
- State<StatefulWidget> createState() {
- return _ChewieDemoState();
- }
- }
- class _ChewieDemoState extends State<ChewieDemo> {
- TargetPlatform _platform;
- VideoPlayerController _videoPlayerController1;
- VideoPlayerController _videoPlayerController2;
- ChewieController _chewieController;
- double aspectRatio=1;
- CartoonMaterialControls _i2materialControls;
- List<MoreVideo> lists = [];
- int currentVideoIndex=0;
- Future<void> _initializeVideoPlayerFuture;
- bool _disposed = false;
- var _isPlaying = false;
- var _isEndPlaying = false;
- bool _firstFullScreen = true;
- @override
- void initState() {
- super.initState();
- Future.delayed(Duration(milliseconds: 1000))
- .then((_){
- setState(() {
- lists.addAll([
- 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),
- MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
- 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),
- MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
- 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),
- MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
- 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),
- MoreVideo("0", "http://www.i2edu.cn/images/pic04.png", "https://v-cdn.zjol.com.cn/276985.mp4", Duration(seconds: 1200), false),
- ]);
- });
- _i2materialControls = CartoonMaterialControls(
- enableQuickControl: true,
- moreVideo: lists,
- onTapMoreVideo: (index) {
- print("onTapMoreVideo $index");
- _startPlay(index);
- },
- onTapPrevious: () {
- if (currentVideoIndex - 1 < 0) return;
- _startPlay(currentVideoIndex - 1);
- },
- onTapNext: () {
- if (currentVideoIndex + 1 >= lists.length) return;
- _startPlay(currentVideoIndex + 1);
- },
- onTapLove: () {
- },
- );
- _initializePlay(0);
- });
- _videoPlayerController2 = VideoPlayerController.network(
- 'https://v-cdn.zjol.com.cn/276985.mp4')
- ..initialize().then((_){
- setState(() {
- });
- });
- }
- @override
- void dispose() {
- _disposed = true;
- _videoPlayerController1.dispose();
- _videoPlayerController2.dispose();
- _chewieController.dispose();
- super.dispose();
- }
- void delayEnterFullScreen() async {
- Future.delayed(Duration(milliseconds: 100))
- .then((_){
- _chewieController.toggleFullScreen();
- });
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: widget.title,
- theme: ThemeData.light().copyWith(
- platform: _platform ?? Theme.of(context).platform,
- ),
- home: Scaffold(
- backgroundColor: Colors.black,
- body: Column(
- children: <Widget>[
- Stack(
- children: <Widget>[
- Container(
- // constraints: BoxConstraints(maxHeight: 200),
- child: _playView(),
- color: Colors.black,
- ),
- ],
- )
- ],
- ),
- ),
- );
- }
- Future<bool> _clearPrevious() async {
- await _videoPlayerController1?.pause();
- _videoPlayerController1?.removeListener(_controllerListener);
- return true;
- }
- // tracking status
- void _controllerListener() {
- if (_videoPlayerController1 == null || _disposed) {
- return;
- }
- if (!_videoPlayerController1.value.initialized) {
- return;
- }
- final position = _videoPlayerController1.value.position;
- final duration = _videoPlayerController1.value.duration;
- if (duration != null && position != null) {
- final isPlaying = position.inMilliseconds < duration.inMilliseconds;
- final isEndPlaying = position.inMilliseconds > 0 &&
- position.inSeconds == duration.inSeconds;
- if (_isPlaying != isPlaying || _isEndPlaying != isEndPlaying) {
- _isPlaying = isPlaying;
- _isEndPlaying = isEndPlaying;
- print(
- "$currentVideoIndex -----> isPlaying=$isPlaying / isCompletePlaying=$isEndPlaying");
- if (isEndPlaying) {
- final isComplete = currentVideoIndex == lists.length - 1;
- if (isComplete) {
- print("played all!!");
- } else {
- _startPlay(currentVideoIndex + 1);
- }
- }
- }
- }
- }
- Future<void> _startPlay(int index) async {
- print("play ---------> $index");
- setState(() {
- _initializeVideoPlayerFuture = null;
- });
- Future.delayed(const Duration(milliseconds: 200), () {
- _clearPrevious().then((_){
- _initializePlay(index);
- });
- });
- }
- Future<void> _initializePlay(int index) async {
- _videoPlayerController1 = VideoPlayerController.network(lists[index].videoUrl);
- _videoPlayerController1.addListener(_controllerListener);
- _chewieController = ChewieController(
- videoPlayerController: _videoPlayerController1,
- customControls: _i2materialControls,
- fullScreenByDefault: true,
- forceFullScreen: true,
- allowedScreenSleep: false,
- showControls: true,
- // showControlsOnInitialize: true,
- );
- _chewieController?.addListener((){
- if (_videoPlayerController1 != null && !_chewieController.isFullScreen) {
- Navigator.of(context).maybePop();
- }
- });
- _initializeVideoPlayerFuture = _videoPlayerController1.initialize();
- setState(() {
- currentVideoIndex = index;
- for (int i = 0; i < lists.length; i++) {
- lists[i].isSelect = (i == index);
- }
- });
- }
- // play view area
- Widget _playView() {
- // FutureBuilder to display a loading spinner until finishes initializing
- return FutureBuilder(
- future: _initializeVideoPlayerFuture,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- if (snapshot.connectionState == ConnectionState.done) {
- _chewieController.play();
- return AspectRatio(
- aspectRatio: _videoPlayerController1.value.aspectRatio,
- child: Chewie(controller: _chewieController),
- );
- } else {
- return SizedBox(
- height: MediaQuery.of(context).size.height,
- child: Center(child: CircularProgressIndicator()),
- );
- }
- },
- );
- }
- }
|