-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathapp.dart
383 lines (359 loc) · 11.5 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import 'dart:io';
import 'package:chewie/chewie.dart';
import 'package:chewie_example/app/theme.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
class ChewieDemo extends StatefulWidget {
const ChewieDemo({
super.key,
this.title = 'Chewie Demo',
});
final String title;
@override
State<StatefulWidget> createState() {
return _ChewieDemoState();
}
}
class _ChewieDemoState extends State<ChewieDemo> {
TargetPlatform? _platform;
late VideoPlayerController _videoPlayerController1;
late VideoPlayerController _videoPlayerController2;
ChewieController? _chewieController;
int? bufferDelay;
@override
void initState() {
super.initState();
initializePlayer();
}
@override
void dispose() {
_videoPlayerController1.dispose();
_videoPlayerController2.dispose();
_chewieController?.dispose();
super.dispose();
}
List<String> srcs = [
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4",
"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4",
];
Future<void> initializePlayer() async {
_videoPlayerController1 =
VideoPlayerController.networkUrl(Uri.parse(srcs[currPlayIndex]));
_videoPlayerController2 =
VideoPlayerController.networkUrl(Uri.parse(srcs[currPlayIndex]));
await Future.wait([
_videoPlayerController1.initialize(),
_videoPlayerController2.initialize()
]);
_createChewieController();
setState(() {});
}
void _createChewieController() {
// final subtitles = [
// Subtitle(
// index: 0,
// start: Duration.zero,
// end: const Duration(seconds: 10),
// text: 'Hello from subtitles',
// ),
// Subtitle(
// index: 0,
// start: const Duration(seconds: 10),
// end: const Duration(seconds: 20),
// text: 'Whats up? :)',
// ),
// ];
final subtitles = [
Subtitle(
index: 0,
start: Duration.zero,
end: const Duration(seconds: 10),
text: const TextSpan(
children: [
TextSpan(
text: 'Hello',
style: TextStyle(color: Colors.red, fontSize: 22),
),
TextSpan(
text: ' from ',
style: TextStyle(color: Colors.green, fontSize: 20),
),
TextSpan(
text: 'subtitles',
style: TextStyle(color: Colors.blue, fontSize: 18),
)
],
),
),
Subtitle(
index: 0,
start: const Duration(seconds: 10),
end: const Duration(seconds: 20),
text: 'Whats up? :)',
// text: const TextSpan(
// text: 'Whats up? :)',
// style: TextStyle(color: Colors.amber, fontSize: 22, fontStyle: FontStyle.italic),
// ),
),
];
_chewieController = ChewieController(
videoPlayerController: _videoPlayerController1,
autoPlay: true,
looping: true,
progressIndicatorDelay:
bufferDelay != null ? Duration(milliseconds: bufferDelay!) : null,
additionalOptions: (context) {
return <OptionItem>[
OptionItem(
onTap: (context) => toggleVideo(),
iconData: Icons.live_tv_sharp,
title: 'Toggle Video Src',
),
];
},
subtitle: Subtitles(subtitles),
showSubtitles: true,
subtitleBuilder: (context, dynamic subtitle) => Container(
padding: const EdgeInsets.all(10.0),
child: subtitle is InlineSpan
? RichText(
text: subtitle,
)
: Text(
subtitle.toString(),
style: const TextStyle(color: Colors.black),
),
),
hideControlsTimer: const Duration(seconds: 1),
// Try playing around with some of these other options:
// showControls: false,
// materialProgressColors: ChewieProgressColors(
// playedColor: Colors.red,
// handleColor: Colors.blue,
// backgroundColor: Colors.grey,
// bufferedColor: Colors.lightGreen,
// ),
// placeholder: Container(
// color: Colors.grey,
// ),
// autoInitialize: true,
);
}
int currPlayIndex = 0;
Future<void> toggleVideo() async {
await _videoPlayerController1.pause();
currPlayIndex += 1;
if (currPlayIndex >= srcs.length) {
currPlayIndex = 0;
}
await initializePlayer();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: widget.title,
theme: AppTheme.light.copyWith(
platform: _platform ?? Theme.of(context).platform,
),
home: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
children: <Widget>[
Expanded(
child: Center(
child: _chewieController != null &&
_chewieController!
.videoPlayerController.value.isInitialized
? Chewie(
controller: _chewieController!,
)
: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
SizedBox(height: 20),
Text('Loading'),
],
),
),
),
TextButton(
onPressed: () {
_chewieController?.enterFullScreen();
},
child: const Text('Fullscreen'),
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_videoPlayerController1.pause();
_videoPlayerController1.seekTo(Duration.zero);
_createChewieController();
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Landscape Video"),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_videoPlayerController2.pause();
_videoPlayerController2.seekTo(Duration.zero);
_chewieController = _chewieController!.copyWith(
videoPlayerController: _videoPlayerController2,
autoPlay: true,
looping: true,
/* subtitle: Subtitles([
Subtitle(
index: 0,
start: Duration.zero,
end: const Duration(seconds: 10),
text: 'Hello from subtitles',
),
Subtitle(
index: 0,
start: const Duration(seconds: 10),
end: const Duration(seconds: 20),
text: 'Whats up? :)',
),
]),
subtitleBuilder: (context, subtitle) => Container(
padding: const EdgeInsets.all(10.0),
child: Text(
subtitle,
style: const TextStyle(color: Colors.white),
),
), */
);
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Portrait Video"),
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_platform = TargetPlatform.android;
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Android controls"),
),
),
),
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_platform = TargetPlatform.iOS;
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("iOS controls"),
),
),
)
],
),
Row(
children: <Widget>[
Expanded(
child: TextButton(
onPressed: () {
setState(() {
_platform = TargetPlatform.windows;
});
},
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 16.0),
child: Text("Desktop controls"),
),
),
),
],
),
if (Platform.isAndroid)
ListTile(
title: const Text("Delay"),
subtitle: DelaySlider(
delay:
_chewieController?.progressIndicatorDelay?.inMilliseconds,
onSave: (delay) async {
if (delay != null) {
bufferDelay = delay == 0 ? null : delay;
await initializePlayer();
}
},
),
)
],
),
),
);
}
}
class DelaySlider extends StatefulWidget {
const DelaySlider({super.key, required this.delay, required this.onSave});
final int? delay;
final void Function(int?) onSave;
@override
State<DelaySlider> createState() => _DelaySliderState();
}
class _DelaySliderState extends State<DelaySlider> {
int? delay;
bool saved = false;
@override
void initState() {
super.initState();
delay = widget.delay;
}
@override
Widget build(BuildContext context) {
const int max = 1000;
return ListTile(
title: Text(
"Progress indicator delay ${delay != null ? "${delay.toString()} MS" : ""}",
),
subtitle: Slider(
value: delay != null ? (delay! / max) : 0,
onChanged: (value) async {
delay = (value * max).toInt();
setState(() {
saved = false;
});
},
),
trailing: IconButton(
icon: const Icon(Icons.save),
onPressed: saved
? null
: () {
widget.onSave(delay);
setState(() {
saved = true;
});
},
),
);
}
}