-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[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
[video_player] Move Android buffer updates to Dart #9771
Conversation
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.
Code Review
This pull request is a nice refactoring that moves the buffer update logic from the native Android side to Dart. This simplifies the native code and improves efficiency by bundling playback and buffer position updates into a single platform channel call. The introduction of the _PlayerInstance
class is a great structural improvement for managing per-player state in Dart.
I have a couple of suggestions for minor improvements: one to align a test method name with Java conventions, and another to remove what appears to be dead code following the refactoring. Overall, this is a solid change.
'bufferingUpdate' => VideoEvent( | ||
eventType: VideoEventType.bufferingUpdate, | ||
buffered: _bufferRangeForPosition(map['position'] as int), | ||
), |
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.
This bufferingUpdate
case appears to be unreachable. With the refactoring in this PR, the logic for sending buffer updates has been moved from the native Java code to the getPosition
method in Dart. The native side should no longer be sending 'bufferingUpdate'
events, making this case dead code. It can be safely removed.
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.
This is not correct; the Java code still sends this when the player enters STATE_BUFFERING
.
...deo_player_android/android/src/test/java/io/flutter/plugins/videoplayer/VideoPlayerTest.java
Outdated
Show resolved
Hide resolved
), | ||
_ => VideoEvent(eventType: VideoEventType.unknown), | ||
}; | ||
}); |
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.
This switch is moved almost unchanged to the player instance class below.
|
||
// Turns a single buffer position, which is what ExoPlayer reports, into the | ||
// DurationRange array expected by [VideoEventType.bufferingUpdate]. | ||
List<DurationRange> _bufferRangeForPosition(int milliseconds) { |
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.
This was extracted from the switch to allow it to be used in _updateBufferingState
as well.
// that the app-facing package polls getPosition frequently, which makes | ||
// this fragile (for instance, as of writing, this won't be called while | ||
// the video is paused). It should instead be called on its own timer, | ||
// independent of higher-level package logic. |
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.
I've expanded on the explanation relative to the version removed from VideoPlayer.java, but it's still fundamentally the same hack as the Java version was for now (to minimize behavioral changes in this PR).
@@ -50,7 +60,12 @@ abstract class VideoPlayerInstanceApi { | |||
void setVolume(double volume); | |||
void setPlaybackSpeed(double speed); | |||
void play(); | |||
int getPosition(); | |||
void seekTo(int position); |
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 specify that the position is also in milliseconds here?
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.
Good call; added docs to all of these methods.
void setVolume(double volume); | ||
void setPlaybackSpeed(double speed); |
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.
I think it might help to provide what that max/min bounds of these values would be or at least specify the unit.
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.
Added.
VideoViewType.platformView => const _VideoPlayerPlatformViewState(), | ||
}; | ||
final String eventChannelName = | ||
'flutter.io/videoPlayer/videoEvents$playerId'; |
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.
flutter/packages@09533b7...5c52c55 2025-08-15 [email protected] [video_player] Move Android buffer updates to Dart (flutter/packages#9771) 2025-08-15 [email protected] [webview_flutter] Add support for payment requests on Android (flutter/packages#9679) 2025-08-15 [email protected] [vector_graphics_compiler] Set the m4_10 (Z scale) value to 1 when constructing an AffineMatrix from an SVG matrix (flutter/packages#9813) 2025-08-15 [email protected] [url_launcher_ios] Fix test button text to work on iOS 26 (flutter/packages#9766) 2025-08-15 [email protected] [video_player] Simplify native iOS code (flutter/packages#9800) 2025-08-14 [email protected] [image_picker] Add the ability to pick multiple videos - platform_interface (flutter/packages#9804) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages-flutter-autoroll Please CC [email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Since ExoPlayer does not generate events for changes to the buffer position, the
video_player_android
synthesizes them via polling. This moves the logic to generate these events from Java to Dart, to reduce the amount of logic on the Java side.This does not attempt to fix the structural problem that the workaround was piggy-backed on the fact that the app-facing package polls frequently for the playback position, generating buffer update events when that polling happens rather than via any independent internal polling system. This approach is not only fragile, as it depends on logic defined in another package, but already doesn't work in the case of a paused video. However, this refactoring will make it possible to change this behavior in Dart code in a follow-up focused on that behavioral change. This change should be behavior-neutral, except that it adds the simple improvement of not creating events if the buffer position hasn't changed, so that clients aren't getting a stream of useless buffer position events during playback.
This version will also be more efficient from a platform channel standpoint, since instead of sending a separate message with the buffer position, it's returned in the same call as the playback position.
To support synthesizing events on the Dart side, this adds a custom stream controller in Dart, instead of creating it directly from the EventChannel stream; this change will make future changes to a non-method-channel approach (e.g., FFI callbacks) straightforward. As this adds non-trivial additional per-player-instance state on the Dart side, this adds a Dart player instance class, and consolidates existing state into that class, including passthrough methods for the API calls, making it clearer which state is plugin level vs player level.
Part of flutter/flutter#172763
Pre-Review Checklist
[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///
).Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assist
bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3