-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[video_player] Move Android buffer updates to Dart #9771
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
Changes from 1 commit
5b227a4
fa87a01
2f3a4df
1f88457
fc1cd2a
395b7ba
796ab14
93f36e3
6d5e1a9
c328f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,9 @@ class AndroidVideoPlayer extends VideoPlayerPlatform { | |
|
|
||
| @override | ||
| Future<void> dispose(int playerId) async { | ||
| final _PlayerInstance? player = _players.remove(playerId); | ||
| await _api.dispose(playerId); | ||
| _players.remove(playerId); | ||
| await player?.dispose(); | ||
| } | ||
|
|
||
| @override | ||
|
|
@@ -132,7 +133,13 @@ class AndroidVideoPlayer extends VideoPlayerPlatform { | |
| ), | ||
| VideoViewType.platformView => const _VideoPlayerPlatformViewState(), | ||
| }; | ||
| return _PlayerInstance(_playerProvider(playerId), viewState); | ||
| final String eventChannelName = | ||
| 'flutter.io/videoPlayer/videoEvents$playerId'; | ||
| return _PlayerInstance( | ||
| _playerProvider(playerId), | ||
| viewState, | ||
| eventChannelName: eventChannelName, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -176,41 +183,7 @@ class AndroidVideoPlayer extends VideoPlayerPlatform { | |
|
|
||
| @override | ||
| Stream<VideoEvent> videoEventsFor(int playerId) { | ||
| return _eventChannelFor(playerId).receiveBroadcastStream().map(( | ||
| dynamic event, | ||
| ) { | ||
| final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>; | ||
| return switch (map['event']) { | ||
| 'initialized' => VideoEvent( | ||
| eventType: VideoEventType.initialized, | ||
| duration: Duration(milliseconds: map['duration'] as int), | ||
| size: Size( | ||
| (map['width'] as num?)?.toDouble() ?? 0.0, | ||
| (map['height'] as num?)?.toDouble() ?? 0.0, | ||
| ), | ||
| rotationCorrection: map['rotationCorrection'] as int? ?? 0, | ||
| ), | ||
| 'completed' => VideoEvent(eventType: VideoEventType.completed), | ||
| 'bufferingUpdate' => VideoEvent( | ||
| eventType: VideoEventType.bufferingUpdate, | ||
| buffered: <DurationRange>[ | ||
| DurationRange( | ||
| Duration.zero, | ||
| Duration(milliseconds: map['position'] as int), | ||
| ), | ||
| ], | ||
| ), | ||
| 'bufferingStart' => VideoEvent( | ||
| eventType: VideoEventType.bufferingStart, | ||
| ), | ||
| 'bufferingEnd' => VideoEvent(eventType: VideoEventType.bufferingEnd), | ||
| 'isPlayingStateUpdate' => VideoEvent( | ||
| eventType: VideoEventType.isPlayingStateUpdate, | ||
| isPlaying: map['isPlaying'] as bool, | ||
| ), | ||
| _ => VideoEvent(eventType: VideoEventType.unknown), | ||
| }; | ||
| }); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This switch is moved almost unchanged to the player instance class below. |
||
| return _playerWith(id: playerId).videoEvents(); | ||
| } | ||
|
|
||
| @override | ||
|
|
@@ -236,10 +209,6 @@ class AndroidVideoPlayer extends VideoPlayerPlatform { | |
| return _api.setMixWithOthers(mixWithOthers); | ||
| } | ||
|
|
||
| EventChannel _eventChannelFor(int playerId) { | ||
| return EventChannel('flutter.io/videoPlayer/videoEvents$playerId'); | ||
| } | ||
|
|
||
| _PlayerInstance _playerWith({required int id}) { | ||
| final _PlayerInstance? player = _players[id]; | ||
| return player ?? (throw StateError('No active player with ID $id.')); | ||
|
|
@@ -274,9 +243,25 @@ PlatformVideoViewType _platformVideoViewTypeFromVideoViewType( | |
| class _PlayerInstance { | ||
| /// Creates a new instance of [_PlayerInstance] corresponding to the given | ||
| /// API instance. | ||
| _PlayerInstance(this._api, this.viewState); | ||
| _PlayerInstance( | ||
| this._api, | ||
| this.viewState, { | ||
| required String eventChannelName, | ||
| }) { | ||
| _eventChannel = EventChannel(eventChannelName); | ||
| _eventSubscription = _eventChannel.receiveBroadcastStream().listen( | ||
| _onStreamEvent, | ||
| onError: (Object e) { | ||
| _eventStreamController.addError(e); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| final VideoPlayerInstanceApi _api; | ||
| late final EventChannel _eventChannel; | ||
| final StreamController<VideoEvent> _eventStreamController = | ||
| StreamController<VideoEvent>.broadcast(); | ||
| late final StreamSubscription<dynamic> _eventSubscription; | ||
|
|
||
| final _VideoPlayerViewState viewState; | ||
|
|
||
|
|
@@ -307,6 +292,49 @@ class _PlayerInstance { | |
| Future<int> getPosition() { | ||
| return _api.getPosition(); | ||
| } | ||
|
|
||
| Stream<VideoEvent> videoEvents() { | ||
| return _eventStreamController.stream; | ||
| } | ||
|
|
||
| Future<void> dispose() async { | ||
| await _eventSubscription.cancel(); | ||
| } | ||
|
|
||
| void _onStreamEvent(dynamic event) { | ||
| final Map<dynamic, dynamic> map = event as Map<dynamic, dynamic>; | ||
| _eventStreamController.add(switch (map['event']) { | ||
| 'initialized' => VideoEvent( | ||
| eventType: VideoEventType.initialized, | ||
| duration: Duration(milliseconds: map['duration'] as int), | ||
| size: Size( | ||
| (map['width'] as num?)?.toDouble() ?? 0.0, | ||
| (map['height'] as num?)?.toDouble() ?? 0.0, | ||
| ), | ||
| rotationCorrection: map['rotationCorrection'] as int? ?? 0, | ||
| ), | ||
| 'completed' => VideoEvent(eventType: VideoEventType.completed), | ||
| 'bufferingUpdate' => VideoEvent( | ||
| eventType: VideoEventType.bufferingUpdate, | ||
| buffered: _bufferRangeForPosition(map['position'] as int), | ||
| ), | ||
|
Comment on lines
+346
to
+349
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct; the Java code still sends this when the player enters |
||
| 'bufferingStart' => VideoEvent(eventType: VideoEventType.bufferingStart), | ||
| 'bufferingEnd' => VideoEvent(eventType: VideoEventType.bufferingEnd), | ||
| 'isPlayingStateUpdate' => VideoEvent( | ||
| eventType: VideoEventType.isPlayingStateUpdate, | ||
| isPlaying: map['isPlaying'] as bool, | ||
| ), | ||
| _ => VideoEvent(eventType: VideoEventType.unknown), | ||
| }); | ||
| } | ||
|
|
||
| // Turns a single buffer position, which is what ExoPlayer reports, into the | ||
| // DurationRange array expected by [VideoEventType.bufferingUpdate]. | ||
| List<DurationRange> _bufferRangeForPosition(int milliseconds) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was extracted from the switch to allow it to be used in |
||
| return <DurationRange>[ | ||
| DurationRange(Duration.zero, Duration(milliseconds: milliseconds)), | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| /// Base class representing the state of a video player view. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make the magic string a constant outside of this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted, and added a comment about the value it has to match on the other side.