Skip to content

Commit e7e1065

Browse files
cbenhagenEgor
authored andcommitted
[video_player] Make sure the plugin is correctly initialized (flutter#2434)
The initialization was done through a static initialization of an unused field, and since static initialization is invoked lazily it was never initialized.
1 parent d50ad44 commit e7e1065

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

packages/video_player/video_player/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.5+2
2+
3+
* Make sure the plugin is correctly initialized
4+
15
## 0.10.5+1
26

37
* Fixes issue where `initialize()` `Future` stalls when failing to load source

packages/video_player/video_player/lib/video_player.dart

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import 'package:video_player_platform_interface/video_player_platform_interface.
1414
export 'package:video_player_platform_interface/video_player_platform_interface.dart'
1515
show DurationRange, DataSourceType, VideoFormat;
1616

17-
// This will clear all open videos on the platform when a full restart is
18-
// performed.
19-
// ignore: unused_element
20-
final VideoPlayerPlatform _ = VideoPlayerPlatform.instance..init();
17+
final VideoPlayerPlatform _videoPlayerPlatform = VideoPlayerPlatform.instance
18+
// This will clear all open videos on the platform when a full restart is
19+
// performed.
20+
..init();
2121

2222
/// The duration, current position, buffering state, error state and settings
2323
/// of a [VideoPlayerController].
@@ -229,8 +229,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
229229
);
230230
break;
231231
}
232-
_textureId =
233-
await VideoPlayerPlatform.instance.create(dataSourceDescription);
232+
_textureId = await _videoPlayerPlatform.create(dataSourceDescription);
234233
_creatingCompleter.complete(null);
235234
final Completer<void> initializingCompleter = Completer<void>();
236235

@@ -277,7 +276,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
277276
}
278277
}
279278

280-
_eventSubscription = VideoPlayerPlatform.instance
279+
_eventSubscription = _videoPlayerPlatform
281280
.videoEventsFor(_textureId)
282281
.listen(eventListener, onError: errorListener);
283282
return initializingCompleter.future;
@@ -291,7 +290,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
291290
_isDisposed = true;
292291
_timer?.cancel();
293292
await _eventSubscription?.cancel();
294-
await VideoPlayerPlatform.instance.dispose(_textureId);
293+
await _videoPlayerPlatform.dispose(_textureId);
295294
}
296295
_lifeCycleObserver.dispose();
297296
}
@@ -326,15 +325,15 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
326325
if (!value.initialized || _isDisposed) {
327326
return;
328327
}
329-
await VideoPlayerPlatform.instance.setLooping(_textureId, value.isLooping);
328+
await _videoPlayerPlatform.setLooping(_textureId, value.isLooping);
330329
}
331330

332331
Future<void> _applyPlayPause() async {
333332
if (!value.initialized || _isDisposed) {
334333
return;
335334
}
336335
if (value.isPlaying) {
337-
await VideoPlayerPlatform.instance.play(_textureId);
336+
await _videoPlayerPlatform.play(_textureId);
338337
_timer = Timer.periodic(
339338
const Duration(milliseconds: 500),
340339
(Timer timer) async {
@@ -350,23 +349,23 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
350349
);
351350
} else {
352351
_timer?.cancel();
353-
await VideoPlayerPlatform.instance.pause(_textureId);
352+
await _videoPlayerPlatform.pause(_textureId);
354353
}
355354
}
356355

357356
Future<void> _applyVolume() async {
358357
if (!value.initialized || _isDisposed) {
359358
return;
360359
}
361-
await VideoPlayerPlatform.instance.setVolume(_textureId, value.volume);
360+
await _videoPlayerPlatform.setVolume(_textureId, value.volume);
362361
}
363362

364363
/// The position in the current video.
365364
Future<Duration> get position async {
366365
if (_isDisposed) {
367366
return null;
368367
}
369-
return await VideoPlayerPlatform.instance.getPosition(_textureId);
368+
return await _videoPlayerPlatform.getPosition(_textureId);
370369
}
371370

372371
/// Sets the video's current timestamp to be at [moment]. The next
@@ -383,7 +382,7 @@ class VideoPlayerController extends ValueNotifier<VideoPlayerValue> {
383382
} else if (position < const Duration()) {
384383
position = const Duration();
385384
}
386-
await VideoPlayerPlatform.instance.seekTo(_textureId, position);
385+
await _videoPlayerPlatform.seekTo(_textureId, position);
387386
value = value.copyWith(position: position);
388387
}
389388

@@ -483,7 +482,7 @@ class _VideoPlayerState extends State<VideoPlayer> {
483482
Widget build(BuildContext context) {
484483
return _textureId == null
485484
? Container()
486-
: VideoPlayerPlatform.instance.buildView(_textureId);
485+
: _videoPlayerPlatform.buildView(_textureId);
487486
}
488487
}
489488

packages/video_player/video_player/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: video_player
22
description: Flutter plugin for displaying inline video with other Flutter
33
widgets on Android and iOS.
4-
version: 0.10.5+1
4+
version: 0.10.5+2
55
homepage: https://github.com/flutter/plugins/tree/master/packages/video_player/video_player
66

77
flutter:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2019 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/widgets.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:video_player/video_player.dart';
8+
9+
import 'video_player_test.dart' show FakeVideoPlayerPlatform;
10+
11+
void main() {
12+
// This test needs to run first and therefore needs to be the only test
13+
// in this file.
14+
test('plugin initialized', () async {
15+
WidgetsFlutterBinding.ensureInitialized();
16+
FakeVideoPlayerPlatform fakeVideoPlayerPlatform = FakeVideoPlayerPlatform();
17+
18+
final VideoPlayerController controller = VideoPlayerController.network(
19+
'https://127.0.0.1',
20+
);
21+
await controller.initialize();
22+
expect(fakeVideoPlayerPlatform.calls.first.method, 'init');
23+
});
24+
}

0 commit comments

Comments
 (0)