-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
138 lines (113 loc) · 3.27 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* README
* Flutter: 2.5.3
* Dart: 2.14.4
* Lib: https://pub.dev/packages/video_player
*/
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(
MaterialApp(
home: VideoPlayerDemo(),
),
);
}
class VideoPlayerDemo extends StatefulWidget {
@override
_VideoPlayerDemoState createState() => _VideoPlayerDemoState();
}
class _VideoPlayerDemoState extends State<VideoPlayerDemo> {
int _index = 0;
final bool _isLooping = true;
final Map<String, VideoPlayerController> _controllers = {};
final Map<int, VoidCallback> _listeners = {};
final List<String> _urls = [
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerJoyrides.mp4',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4',
'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerMeltdowns.mp4',
];
@override
void initState() {
super.initState();
_initVideoPlayer();
}
void _initVideoPlayer() {
_index = 0;
_controllers.clear();
_listeners.clear();
if (_urls.length > 0) {
_initController(0).then((_) {
_playController(0);
});
}
if (_urls.length > 1) {
_initController(1);
}
}
Future<void> _initController(int index) async {
final controller = VideoPlayerController.network(_urls[index]);
_controllers[_urls[index]] = controller;
await controller.initialize();
}
void _playController(int index) async {
if (!_listeners.keys.contains(index)) {
_listeners[index] = _listenerVideoPlayer(index);
}
_controller(index).addListener(_listeners[index] as Function());
await _controller(index).play();
setState(() {});
}
VideoPlayerController _controller(int index) {
return _controllers[_urls[index]] as VideoPlayerController;
}
VoidCallback _listenerVideoPlayer(index) {
return () {
final int dur = _controller(index).value.duration.inMilliseconds;
final int pos = _controller(index).value.position.inMilliseconds;
if (dur - pos > 1) {
return;
}
if (index < _urls.length - 1) {
return _nextVideo();
}
if (_isLooping) {
_stopController(index);
_removeController(index);
_initVideoPlayer();
}
};
}
void _nextVideo() async {
if (_index == _urls.length - 1) {
return;
}
_stopController(_index);
_removeController(_index);
_playController(++_index);
if (_index + 1 > _urls.length - 1) {
return;
}
_initController(_index + 1);
}
void _stopController(int index) {
_controller(index).removeListener(_listeners[index] as Function());
_controller(index).pause();
_controller(index).seekTo(const Duration(milliseconds: 0));
}
void _removeController(int index) {
_controller(index).dispose();
_controllers.remove(_urls[index]);
_listeners.remove(index);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Playing ${_index + 1} of ${_urls.length}"),
),
body: Center(child: VideoPlayer(_controller(_index))),
);
}
}