Skip to content

Commit f5a54e3

Browse files
FirentisTFWOrtes
authored andcommitted
[video_player] Platform view support (flutter#8810)
This PR adds support for platform views as on optional way of displaying a video (as an alternative to Flutter's `Texture` widget). Texture-based approach is still the default setting when creating a new player. Platform interface was updated in flutter#8453 Platform implementations were added in these PRs: - iOS: flutter#8237 - Android: flutter#8466 Closes [flutter/issues/86613](flutter/flutter#86613).
1 parent 4322645 commit f5a54e3

File tree

7 files changed

+258
-89
lines changed

7 files changed

+258
-89
lines changed

packages/video_player/video_player/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.10.0
22

3+
* Adds support for platform views as an optional way of displaying a video on Android and iOS.
34
* Updates README to indicate that Andoid SDK <21 is no longer supported.
45
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
56

packages/video_player/video_player/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,10 @@ and so on.
140140
To learn about playback speed limitations, see the [`setPlaybackSpeed` method documentation](https://pub.dev/documentation/video_player/latest/video_player/VideoPlayerController/setPlaybackSpeed.html).
141141

142142
Furthermore, see the example app for an example playback speed implementation.
143+
144+
### Video view type
145+
146+
You can set the video view type of your controller (instance of `VideoPlayerController`) during its creation by passing the `videoViewType` argument.
147+
If set to `VideoViewType.platformView`, platform views will be used instead of texture view on supported platforms.
148+
149+
The relative performance of the different view types may vary by platform, and on some platforms the use of platform views may have correctness issues in certain circumstances due to limitations of Flutter's platform view system.

packages/video_player/video_player/example/lib/main.dart

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,89 @@ class _App extends StatelessWidget {
5656
),
5757
body: TabBarView(
5858
children: <Widget>[
59-
_BumbleBeeRemoteVideo(),
60-
_ButterFlyAssetVideo(),
61-
_ButterFlyAssetVideoInList(),
59+
_ViewTypeTabBar(
60+
builder: (VideoViewType viewType) =>
61+
_BumbleBeeRemoteVideo(viewType),
62+
),
63+
_ViewTypeTabBar(
64+
builder: (VideoViewType viewType) =>
65+
_ButterFlyAssetVideo(viewType),
66+
),
67+
_ViewTypeTabBar(
68+
builder: (VideoViewType viewType) =>
69+
_ButterFlyAssetVideoInList(viewType),
70+
),
6271
],
6372
),
6473
),
6574
);
6675
}
6776
}
6877

78+
class _ViewTypeTabBar extends StatefulWidget {
79+
const _ViewTypeTabBar({
80+
required this.builder,
81+
});
82+
83+
final Widget Function(VideoViewType) builder;
84+
85+
@override
86+
State<_ViewTypeTabBar> createState() => _ViewTypeTabBarState();
87+
}
88+
89+
class _ViewTypeTabBarState extends State<_ViewTypeTabBar>
90+
with SingleTickerProviderStateMixin {
91+
late final TabController _tabController;
92+
93+
@override
94+
void initState() {
95+
super.initState();
96+
_tabController = TabController(length: 2, vsync: this);
97+
}
98+
99+
@override
100+
void dispose() {
101+
_tabController.dispose();
102+
super.dispose();
103+
}
104+
105+
@override
106+
Widget build(BuildContext context) {
107+
return Column(
108+
children: <Widget>[
109+
TabBar(
110+
controller: _tabController,
111+
isScrollable: true,
112+
tabs: const <Widget>[
113+
Tab(
114+
icon: Icon(Icons.texture),
115+
text: 'Texture view',
116+
),
117+
Tab(
118+
icon: Icon(Icons.construction),
119+
text: 'Platform view',
120+
),
121+
],
122+
),
123+
Expanded(
124+
child: TabBarView(
125+
controller: _tabController,
126+
children: <Widget>[
127+
widget.builder(VideoViewType.textureView),
128+
widget.builder(VideoViewType.platformView),
129+
],
130+
),
131+
),
132+
],
133+
);
134+
}
135+
}
136+
69137
class _ButterFlyAssetVideoInList extends StatelessWidget {
138+
const _ButterFlyAssetVideoInList(this.viewType);
139+
140+
final VideoViewType viewType;
141+
70142
@override
71143
Widget build(BuildContext context) {
72144
return ListView(
@@ -90,7 +162,7 @@ class _ButterFlyAssetVideoInList extends StatelessWidget {
90162
alignment: FractionalOffset.bottomRight +
91163
const FractionalOffset(-0.1, -0.1),
92164
children: <Widget>[
93-
_ButterFlyAssetVideo(),
165+
_ButterFlyAssetVideo(viewType),
94166
Image.asset('assets/flutter-mark-square-64.png'),
95167
]),
96168
],
@@ -150,6 +222,10 @@ class _ExampleCard extends StatelessWidget {
150222
}
151223

152224
class _ButterFlyAssetVideo extends StatefulWidget {
225+
const _ButterFlyAssetVideo(this.viewType);
226+
227+
final VideoViewType viewType;
228+
153229
@override
154230
_ButterFlyAssetVideoState createState() => _ButterFlyAssetVideoState();
155231
}
@@ -160,7 +236,10 @@ class _ButterFlyAssetVideoState extends State<_ButterFlyAssetVideo> {
160236
@override
161237
void initState() {
162238
super.initState();
163-
_controller = VideoPlayerController.asset('assets/Butterfly-209.mp4');
239+
_controller = VideoPlayerController.asset(
240+
'assets/Butterfly-209.mp4',
241+
viewType: widget.viewType,
242+
);
164243

165244
_controller.addListener(() {
166245
setState(() {});
@@ -206,6 +285,10 @@ class _ButterFlyAssetVideoState extends State<_ButterFlyAssetVideo> {
206285
}
207286

208287
class _BumbleBeeRemoteVideo extends StatefulWidget {
288+
const _BumbleBeeRemoteVideo(this.viewType);
289+
290+
final VideoViewType viewType;
291+
209292
@override
210293
_BumbleBeeRemoteVideoState createState() => _BumbleBeeRemoteVideoState();
211294
}
@@ -228,6 +311,7 @@ class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
228311
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'),
229312
closedCaptionFile: _loadCaptions(),
230313
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
314+
viewType: widget.viewType,
231315
);
232316

233317
_controller.addListener(() {

0 commit comments

Comments
 (0)