Skip to content

Commit 58931ed

Browse files
authored
Merge pull request fluttercommunity#99 from cbenhagen/ChewieController
ChewieController
2 parents 4ff9e25 + c553696 commit 58931ed

12 files changed

+668
-647
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 0.9.0
4+
5+
* **Breaking changes**: Add a `ChewieController` to make customizations and control from outside of the player easier.
6+
Refer to the [README](README.md) for details on how to upgrade from previous versions.
7+
38
## 0.8.0
49

510
* Update to work with `video_player: ">=0.7.0 <0.8.0` - Thanks @Sub6Resources

README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,63 @@ dependencies:
2121
2222
```dart
2323
import 'package:chewie/chewie.dart';
24+
final videoPlayerController = VideoPlayerController.network(
25+
'https://flutter.github.io/assets-for-api-docs/videos/butterfly.mp4');
2426

25-
final playerWidget = new Chewie(
26-
new VideoPlayerController.network(
27-
'https://flutter.github.io/assets-for-api-docs/videos/butterfly.mp4'
28-
),
27+
final chewieController = ChewieController(
28+
videoPlayerController: videoPlayerController,
2929
aspectRatio: 3 / 2,
3030
autoPlay: true,
3131
looping: true,
3232
);
33+
34+
final playerWidget = Chewie(
35+
controller: chewieController,
36+
);
37+
```
38+
39+
Please make sure to dispose both controller widgets after use. For example by overriding the dispose method of the a `StatefulWidget`:
40+
```dart
41+
@override
42+
void dispose() {
43+
videoPlayerController.dispose();
44+
chewieController.dispose();
45+
super.dispose();
46+
}
3347
```
3448

3549
## Example
3650

3751
Please run the app in the [`example/`](https://github.com/brianegan/chewie/tree/master/example) folder to start playing!
3852

53+
## Migrating from Chewie < 0.9.0
54+
Instead of passing the `VideoPlayerController` and your options to the `Chewie` widget you now pass them to the `ChewieController` and pass that latter to the `Chewie` widget.
55+
56+
```dart
57+
final playerWidget = Chewie(
58+
videoPlayerController,
59+
aspectRatio: 3 / 2,
60+
autoPlay: true,
61+
looping: true,
62+
);
63+
```
64+
65+
becomes
66+
67+
```dart
68+
final chewieController = ChewieController(
69+
videoPlayerController: videoPlayerController,
70+
aspectRatio: 3 / 2,
71+
autoPlay: true,
72+
looping: true,
73+
);
74+
75+
final playerWidget = Chewie(
76+
controller: chewieController,
77+
);
78+
```
3979

40-
## iOS Warning
80+
## iOS warning
4181

4282
The video player plugin used by chewie is not functional on iOS simulators. An iOS device must be used during development/testing. Please refer to this [issue](https://github.com/flutter/flutter/issues/14647).
4383

analysis_options.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ linter:
1010
- hash_and_equals
1111
- iterable_contains_unrelated_type
1212
- list_remove_unrelated_type
13+
- sort_constructors_first
1314
- test_types_in_equals
15+
- unnecessary_new
1416
- unrelated_type_equality_checks
1517
- valid_regexps

example/lib/main.dart

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:chewie/chewie.dart';
2+
import 'package:chewie/src/chewie_player.dart';
23
import 'package:flutter/cupertino.dart';
34
import 'package:flutter/material.dart';
45
import 'package:video_player/video_player.dart';
@@ -10,10 +11,10 @@ void main() {
1011
}
1112

1213
class ChewieDemo extends StatefulWidget {
13-
final String title;
14-
1514
ChewieDemo({this.title = 'Chewie Demo'});
1615

16+
final String title;
17+
1718
@override
1819
State<StatefulWidget> createState() {
1920
return _ChewieDemoState();
@@ -22,16 +23,46 @@ class ChewieDemo extends StatefulWidget {
2223

2324
class _ChewieDemoState extends State<ChewieDemo> {
2425
TargetPlatform _platform;
25-
VideoPlayerController _controller;
26+
VideoPlayerController _videoPlayerController1;
27+
VideoPlayerController _videoPlayerController2;
28+
ChewieController _chewieController;
2629

2730
@override
2831
void initState() {
2932
super.initState();
30-
_controller = VideoPlayerController.network(
31-
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
33+
_videoPlayerController1 = VideoPlayerController.network(
34+
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4');
35+
_videoPlayerController2 = VideoPlayerController.network(
36+
'https://www.sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4');
37+
_chewieController = ChewieController(
38+
videoPlayerController: _videoPlayerController1,
39+
aspectRatio: 3 / 2,
40+
autoPlay: true,
41+
looping: true,
42+
// Try playing around with some of these other options:
43+
44+
// showControls: false,
45+
// materialProgressColors: ChewieProgressColors(
46+
// playedColor: Colors.red,
47+
// handleColor: Colors.blue,
48+
// backgroundColor: Colors.grey,
49+
// bufferedColor: Colors.lightGreen,
50+
// ),
51+
// placeholder: Container(
52+
// color: Colors.grey,
53+
// ),
54+
// autoInitialize: true,
3255
);
3356
}
3457

58+
@override
59+
void dispose() {
60+
_videoPlayerController1.dispose();
61+
_videoPlayerController2.dispose();
62+
_chewieController.dispose();
63+
super.dispose();
64+
}
65+
3566
@override
3667
Widget build(BuildContext context) {
3768
return MaterialApp(
@@ -48,35 +79,30 @@ class _ChewieDemoState extends State<ChewieDemo> {
4879
Expanded(
4980
child: Center(
5081
child: Chewie(
51-
_controller,
52-
aspectRatio: 3 / 2,
53-
autoPlay: true,
54-
looping: true,
55-
56-
// Try playing around with some of these other options:
57-
58-
// showControls: false,
59-
// materialProgressColors: ChewieProgressColors(
60-
// playedColor: Colors.red,
61-
// handleColor: Colors.blue,
62-
// backgroundColor: Colors.grey,
63-
// bufferedColor: Colors.lightGreen,
64-
// ),
65-
// placeholder: Container(
66-
// color: Colors.grey,
67-
// ),
68-
// autoInitialize: true,
82+
controller: _chewieController,
6983
),
7084
),
7185
),
86+
FlatButton(
87+
onPressed: () {
88+
_chewieController.enterFullScreen();
89+
},
90+
child: Text('Fullscreen'),
91+
),
7292
Row(
7393
children: <Widget>[
7494
Expanded(
7595
child: FlatButton(
7696
onPressed: () {
7797
setState(() {
78-
_controller = VideoPlayerController.network(
79-
'https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4',
98+
_chewieController.dispose();
99+
_videoPlayerController2.pause();
100+
_videoPlayerController2.seekTo(Duration(seconds: 0));
101+
_chewieController = ChewieController(
102+
videoPlayerController: _videoPlayerController1,
103+
aspectRatio: 3 / 2,
104+
autoPlay: true,
105+
looping: true,
80106
);
81107
});
82108
},
@@ -90,8 +116,14 @@ class _ChewieDemoState extends State<ChewieDemo> {
90116
child: FlatButton(
91117
onPressed: () {
92118
setState(() {
93-
_controller = VideoPlayerController.network(
94-
'https://www.sample-videos.com/video123/mp4/480/big_buck_bunny_480p_20mb.mp4',
119+
_chewieController.dispose();
120+
_videoPlayerController1.pause();
121+
_videoPlayerController1.seekTo(Duration(seconds: 0));
122+
_chewieController = ChewieController(
123+
videoPlayerController: _videoPlayerController2,
124+
aspectRatio: 3 / 2,
125+
autoPlay: true,
126+
looping: true,
95127
);
96128
});
97129
},

0 commit comments

Comments
 (0)