Skip to content

Commit

Permalink
[video_player] Add a Uri-typed factory method (#3513)
Browse files Browse the repository at this point in the history
This PR improves VideoPlayerController network constructor with the datasource type. The solution follows the comments on the issue [#121927](flutter/flutter#121927).

Fixes [#121927](flutter/flutter#121927)
  • Loading branch information
gabrielokura committed Jun 29, 2023
1 parent a0ea9a3 commit 5d6e48c
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 106 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,10 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

final VideoPlayerController vController = kIsWeb
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
? VideoPlayerController.network(videoFile!.path)
: VideoPlayerController.file(File(videoFile!.path));

Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_android/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

final VideoPlayerController vController = kIsWeb
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
? VideoPlayerController.network(videoFile!.path)
: VideoPlayerController.file(File(videoFile!.path));

Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_android_camerax/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,10 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

final VideoPlayerController vController = kIsWeb
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
? VideoPlayerController.network(videoFile!.path)
: VideoPlayerController.file(File(videoFile!.path));

Expand Down
4 changes: 4 additions & 0 deletions packages/camera/camera_avfoundation/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,10 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
}

final VideoPlayerController vController = kIsWeb
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
? VideoPlayerController.network(videoFile!.path)
: VideoPlayerController.file(File(videoFile!.path));

Expand Down
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class _MyHomePageState extends State<MyHomePage> {
await _disposeVideoController();
late VideoPlayerController controller;
if (kIsWeb) {
// TODO(gabrielokura): remove the ignore once the following line can migrate to
// use VideoPlayerController.networkUrl after the issue is resolved.
// https://github.com/flutter/flutter/issues/121927
// ignore: deprecated_member_use
controller = VideoPlayerController.network(file.path);
} else {
controller = VideoPlayerController.file(File(file.path));
Expand Down
2 changes: 2 additions & 0 deletions packages/video_player/video_player/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ Anton Borries <mail@antonborri.es>
Alex Li <google@alexv525.com>
Rahul Raj <64.rahulraj@gmail.com>
Koen Van Looveren <vanlooverenkoen.dev@gmail.com>
Gabriel Motelevicz Okura <gabriellevicz@gmail.com>
Idowu Tomiwa <tommydprogrammer@gmail.com>
Márton Matuz <matuzmarci@gmail.com>
6 changes: 4 additions & 2 deletions packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## NEXT
## 2.7.0

* Adds an `Uri` typed factory method `VideoPlayerController.networkUrl` to avoid common mistakes with `String` URIs. The method
receives an`Uri` instead of a `String` url.
* Deprecates `VideoPlayerController.network` factory method.
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.

## 2.6.1

* Synchronizes `VideoPlayerValue.isPlaying` with underlying video player.
Expand Down
4 changes: 2 additions & 2 deletions packages/video_player/video_player/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class _VideoAppState extends State<VideoApp> {
@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
_controller = VideoPlayerController.networkUrl(Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'))
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ void main() {
'live stream duration != 0',
(WidgetTester tester) async {
final VideoPlayerController networkController =
VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/hls/bee.m3u8',
VideoPlayerController.networkUrl(
Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/hls/bee.m3u8'),
);
await networkController.initialize();

Expand Down Expand Up @@ -230,8 +231,8 @@ void main() {

group('network videos', () {
setUp(() {
controller = VideoPlayerController.network(
getUrlForAssetAsNetworkSource(_videoAssetKey));
controller = VideoPlayerController.networkUrl(
Uri.parse(getUrlForAssetAsNetworkSource(_videoAssetKey)));
});

testWidgets(
Expand Down
4 changes: 2 additions & 2 deletions packages/video_player/video_player/example/lib/basic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class _VideoAppState extends State<VideoApp> {
@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4')
_controller = VideoPlayerController.networkUrl(Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'))
..initialize().then((_) {
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
setState(() {});
Expand Down
5 changes: 3 additions & 2 deletions packages/video_player/video_player/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ class _BumbleBeeRemoteVideoState extends State<_BumbleBeeRemoteVideo> {
@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4',
_controller = VideoPlayerController.networkUrl(
Uri.parse(
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4'),
closedCaptionFile: _loadCaptions(),
videoPlayerOptions: VideoPlayerOptions(mixWithOthers: true),
);
Expand Down
32 changes: 27 additions & 5 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,16 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
httpHeaders = const <String, String>{},
super(const VideoPlayerValue(duration: Duration.zero));

/// Constructs a [VideoPlayerController] playing a video from obtained from
/// the network.
/// Constructs a [VideoPlayerController] playing a network video.
///
/// The URI for the video is given by the [dataSource] argument.
///
/// The URI for the video is given by the [dataSource] argument and must not be
/// null.
/// **Android only**: The [formatHint] option allows the caller to override
/// the video format detection code.
/// [httpHeaders] option allows to specify HTTP headers.
///
/// [httpHeaders] option allows to specify HTTP headers
/// for the request to the [dataSource].
@Deprecated('Use VideoPlayerController.networkUrl instead')
VideoPlayerController.network(
this.dataSource, {
this.formatHint,
Expand All @@ -282,6 +283,27 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
package = null,
super(const VideoPlayerValue(duration: Duration.zero));

/// Constructs a [VideoPlayerController] playing a network video.
///
/// The URI for the video is given by the [dataSource] argument.
///
/// **Android only**: The [formatHint] option allows the caller to override
/// the video format detection code.
///
/// [httpHeaders] option allows to specify HTTP headers
/// for the request to the [dataSource].
VideoPlayerController.networkUrl(
Uri url, {
this.formatHint,
Future<ClosedCaptionFile>? closedCaptionFile,
this.videoPlayerOptions,
this.httpHeaders = const <String, String>{},
}) : _closedCaptionFileFuture = closedCaptionFile,
dataSource = url.toString(),
dataSourceType = DataSourceType.network,
package = null,
super(const VideoPlayerValue(duration: Duration.zero));

/// Constructs a [VideoPlayerController] playing a video from a file.
///
/// This will load the file from a file:// URI constructed from [file]'s path.
Expand Down
2 changes: 1 addition & 1 deletion packages/video_player/video_player/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for displaying inline video with other Flutter
widgets on Android, iOS, and web.
repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22
version: 2.6.1
version: 2.7.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void main() {
FakeVideoPlayerPlatform();
VideoPlayerPlatform.instance = fakeVideoPlayerPlatform;

final VideoPlayerController controller = VideoPlayerController.network(
'https://127.0.0.1',
final VideoPlayerController controller = VideoPlayerController.networkUrl(
Uri.parse('https://127.0.0.1'),
);
await controller.initialize();
expect(fakeVideoPlayerPlatform.calls.first, 'init');
Expand Down
Loading

0 comments on commit 5d6e48c

Please sign in to comment.