Skip to content

[video_player] Platform view support #8810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT
## 2.10.0

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

## 2.9.5
Expand Down
108 changes: 101 additions & 7 deletions packages/video_player/video_player/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
/// video.
library;

import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

Expand Down Expand Up @@ -55,18 +58,97 @@ class _App extends StatelessWidget {
),
),
body: TabBarView(
children: <Widget>[
_BumbleBeeRemoteVideo(),
_ButterFlyAssetVideo(),
_ButterFlyAssetVideoInList(),
],
children: !kIsWeb && (Platform.isIOS || Platform.isAndroid)
? <Widget>[
_ViewTypeTabBar(
builder: (VideoViewType viewType) =>
_BumbleBeeRemoteVideo(viewType),
),
_ViewTypeTabBar(
builder: (VideoViewType viewType) =>
_ButterFlyAssetVideo(viewType),
),
_ViewTypeTabBar(
builder: (VideoViewType viewType) =>
_ButterFlyAssetVideoInList(viewType),
),
]
// The plugin does not support platform views on other platforms yet.
: const <Widget>[
_BumbleBeeRemoteVideo(VideoViewType.textureView),
_ButterFlyAssetVideo(VideoViewType.textureView),
_ButterFlyAssetVideoInList(VideoViewType.textureView),
],
),
),
);
}
}

class _ViewTypeTabBar extends StatefulWidget {
const _ViewTypeTabBar({
required this.builder,
});

final Widget Function(VideoViewType) builder;

@override
State<_ViewTypeTabBar> createState() => _ViewTypeTabBarState();
}

class _ViewTypeTabBarState extends State<_ViewTypeTabBar>
with SingleTickerProviderStateMixin {
late final TabController _tabController;

@override
void initState() {
super.initState();
_tabController = TabController(length: 2, vsync: this);
}

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
TabBar(
controller: _tabController,
isScrollable: true,
tabs: const <Widget>[
Tab(
icon: Icon(Icons.texture),
text: 'Texture view',
),
Tab(
icon: Icon(Icons.construction),
text: 'Platform view',
),
],
),
Expanded(
child: TabBarView(
controller: _tabController,
children: <Widget>[
widget.builder(VideoViewType.textureView),
widget.builder(VideoViewType.platformView),
],
),
),
],
);
}
}

class _ButterFlyAssetVideoInList extends StatelessWidget {
const _ButterFlyAssetVideoInList(this.viewType);

final VideoViewType viewType;

@override
Widget build(BuildContext context) {
return ListView(
Expand All @@ -90,7 +172,7 @@ class _ButterFlyAssetVideoInList extends StatelessWidget {
alignment: FractionalOffset.bottomRight +
const FractionalOffset(-0.1, -0.1),
children: <Widget>[
_ButterFlyAssetVideo(),
_ButterFlyAssetVideo(viewType),
Image.asset('assets/flutter-mark-square-64.png'),
]),
],
Expand Down Expand Up @@ -150,6 +232,10 @@ class _ExampleCard extends StatelessWidget {
}

class _ButterFlyAssetVideo extends StatefulWidget {
const _ButterFlyAssetVideo(this.viewType);

final VideoViewType viewType;

@override
_ButterFlyAssetVideoState createState() => _ButterFlyAssetVideoState();
}
Expand All @@ -160,7 +246,10 @@ class _ButterFlyAssetVideoState extends State<_ButterFlyAssetVideo> {
@override
void initState() {
super.initState();
_controller = VideoPlayerController.asset('assets/Butterfly-209.mp4');
_controller = VideoPlayerController.asset(
'assets/Butterfly-209.mp4',
viewType: widget.viewType,
);

_controller.addListener(() {
setState(() {});
Expand Down Expand Up @@ -206,6 +295,10 @@ class _ButterFlyAssetVideoState extends State<_ButterFlyAssetVideo> {
}

class _BumbleBeeRemoteVideo extends StatefulWidget {
const _BumbleBeeRemoteVideo(this.viewType);

final VideoViewType viewType;

@override
_BumbleBeeRemoteVideoState createState() => _BumbleBeeRemoteVideoState();
}
Expand All @@ -228,6 +321,7 @@ class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'),
closedCaptionFile: _loadCaptions(),
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
viewType: widget.viewType,
);

_controller.addListener(() {
Expand Down
Loading