Skip to content
Open
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
3 changes: 2 additions & 1 deletion packages/video_player/video_player/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.14.2

* Fixes `dispose` never completing when `initialize` failed to create the native player.
* Updates README to reflect currently supported OS versions for the latest
versions of the endorsed iOS and macOS platform implementations.
* Applications built with older versions of Flutter will continue to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:video_player/video_player.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

testWidgets('dispose completes after initialize fails', (WidgetTester tester) async {
final controller = VideoPlayerController.asset('assets/does_not_exist.mp4');

await expectLater(controller.initialize(), throwsA(isA<PlatformException>()));

// Without a timeout a regression would hang the test instead of failing it.
await controller.dispose().timeout(const Duration(seconds: 10));
});
}
21 changes: 14 additions & 7 deletions packages/video_player/video_player/lib/video_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,17 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
videoPlayerOptions: videoPlayerOptions,
);

if (videoPlayerOptions?.mixWithOthers != null) {
await _videoPlayerPlatform.setMixWithOthers(videoPlayerOptions!.mixWithOthers);
}
try {
if (videoPlayerOptions?.mixWithOthers != null) {
await _videoPlayerPlatform.setMixWithOthers(videoPlayerOptions!.mixWithOthers);
}

_playerId =
(await _videoPlayerPlatform.createWithOptions(creationOptions)) ?? kUninitializedPlayerId;
_creatingCompleter!.complete();
_playerId =
(await _videoPlayerPlatform.createWithOptions(creationOptions)) ?? kUninitializedPlayerId;
} finally {
// dispose() waits for this, so it must complete even if creation fails.
_creatingCompleter!.complete();
}
final initializingCompleter = Completer<void>();

await _videoPlayerPlatform.setPreventsDisplaySleepDuringVideoPlayback(
Expand Down Expand Up @@ -710,7 +714,10 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
_isDisposed = true;
_timer?.cancel();
await _eventSubscription?.cancel();
await _videoPlayerPlatform.dispose(_playerId);
// There is no native player to dispose if creating it failed.
if (_playerId != kUninitializedPlayerId) {
await _videoPlayerPlatform.dispose(_playerId);
}
}
_lifeCycleObserver?.dispose();
}
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, macOS 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.14.1
version: 2.14.2

environment:
sdk: ^3.12.0
Expand Down
16 changes: 16 additions & 0 deletions packages/video_player/video_player/test/video_player_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,17 @@ void main() {
expect(await controller.position, isNull);
});

test('dispose completes after creating the player failed', () async {
final controller = VideoPlayerController.networkUrl(_localhostUri);
final error = PlatformException(code: 'VideoError', message: 'create failed');
fakeVideoPlayerPlatform.forceCreateError = error;

await expectLater(controller.initialize(), throwsA(same(error)));
await controller.dispose();

expect(fakeVideoPlayerPlatform.calls, isNot(contains('dispose')));
});

test('calling dispose() on disposed controller does not throw', () async {
final controller = VideoPlayerController.networkUrl(_localhostUri);
addTearDown(controller.dispose);
Expand Down Expand Up @@ -2068,6 +2079,7 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
final Map<int, StreamController<VideoEvent>> streams = <int, StreamController<VideoEvent>>{};
List<VideoPlayerOptions?> videoPlayerOptions = <VideoPlayerOptions?>[];
bool forceInitError = false;
PlatformException? forceCreateError;
int nextPlayerId = 0;
final Map<int, Duration> _positions = <int, Duration>{};
final Map<int, VideoPlayerWebOptions> webOptions = <int, VideoPlayerWebOptions>{};
Expand Down Expand Up @@ -2095,6 +2107,10 @@ class FakeVideoPlayerPlatform extends VideoPlayerPlatform {
@override
Future<int?> createWithOptions(VideoCreationOptions options) async {
calls.add('createWithOptions');
final PlatformException? createError = forceCreateError;
if (createError != null) {
throw createError;
}
final stream = StreamController<VideoEvent>();
streams[nextPlayerId] = stream;
if (forceInitError) {
Expand Down