This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[video_player] Fix a disposed
VideoPlayerController
throwing an exc…
…eption when being replaced in the `VideoPlayer` (#4344) * fix: do not removeListener if VideoPlayerController is already disposed Co-authored-by: David Iglesias Teixeira <[email protected]>
- Loading branch information
Showing
4 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/video_player/video_player/example/integration_test/controller_swap_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:video_player/video_player.dart'; | ||
|
||
const Duration _playDuration = Duration(seconds: 1); | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
testWidgets( | ||
'can substitute one controller by another without crashing', | ||
(WidgetTester tester) async { | ||
VideoPlayerController controller = VideoPlayerController.network( | ||
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4', | ||
); | ||
VideoPlayerController another = VideoPlayerController.network( | ||
'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4', | ||
); | ||
await controller.initialize(); | ||
await another.initialize(); | ||
await controller.setVolume(0); | ||
await another.setVolume(0); | ||
|
||
final Completer<void> started = Completer(); | ||
final Completer<void> ended = Completer(); | ||
bool startedBuffering = false; | ||
bool endedBuffering = false; | ||
|
||
another.addListener(() { | ||
if (another.value.isBuffering && !startedBuffering) { | ||
startedBuffering = true; | ||
started.complete(); | ||
} | ||
if (startedBuffering && !another.value.isBuffering && !endedBuffering) { | ||
endedBuffering = true; | ||
ended.complete(); | ||
} | ||
}); | ||
|
||
// Inject a widget with `controller`... | ||
await tester.pumpWidget(renderVideoWidget(controller)); | ||
await controller.play(); | ||
await tester.pumpAndSettle(_playDuration); | ||
await controller.pause(); | ||
|
||
// Disposing controller causes the Widget to crash in the next line | ||
// (Issue https://github.com/flutter/flutter/issues/90046) | ||
await controller.dispose(); | ||
|
||
// Now replace it with `another` controller... | ||
await tester.pumpWidget(renderVideoWidget(another)); | ||
await another.play(); | ||
await another.seekTo(const Duration(seconds: 5)); | ||
await tester.pumpAndSettle(_playDuration); | ||
await another.pause(); | ||
|
||
// Expect that `another` played. | ||
expect(another.value.position, | ||
(Duration position) => position > const Duration(seconds: 0)); | ||
|
||
await started; | ||
expect(startedBuffering, true); | ||
|
||
await ended; | ||
expect(endedBuffering, true); | ||
}, | ||
skip: !(kIsWeb || defaultTargetPlatform == TargetPlatform.android), | ||
); | ||
} | ||
|
||
Widget renderVideoWidget(VideoPlayerController controller) { | ||
return Material( | ||
elevation: 0, | ||
child: Directionality( | ||
textDirection: TextDirection.ltr, | ||
child: Center( | ||
child: AspectRatio( | ||
key: Key('same'), | ||
aspectRatio: controller.value.aspectRatio, | ||
child: VideoPlayer(controller), | ||
), | ||
), | ||
), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters