-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Move Player.Listener
impl, remove @VisibleForTesting isInitialized
.
#6922
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
101c4cd
Move listener class, remove visibleForTesting isInitialized.
matanlurey 6e0ff4b
++
matanlurey c20c245
++
matanlurey 5f6a892
Merge branch 'main' into video-player-is-initialized-refactor
matanlurey a2a1cdf
++
matanlurey bb07c40
++
matanlurey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
..._android/android/src/main/java/io/flutter/plugins/videoplayer/ExoPlayerEventListener.java
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,101 @@ | ||
// 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. | ||
|
||
package io.flutter.plugins.videoplayer; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.media3.common.PlaybackException; | ||
import androidx.media3.common.Player; | ||
import androidx.media3.common.VideoSize; | ||
import androidx.media3.exoplayer.ExoPlayer; | ||
|
||
final class ExoPlayerEventListener implements Player.Listener { | ||
private final ExoPlayer exoPlayer; | ||
private final VideoPlayerCallbacks events; | ||
private boolean isBuffering = false; | ||
private boolean isInitialized = false; | ||
|
||
ExoPlayerEventListener(ExoPlayer exoPlayer, VideoPlayerCallbacks events) { | ||
this.exoPlayer = exoPlayer; | ||
this.events = events; | ||
} | ||
|
||
private void setBuffering(boolean buffering) { | ||
if (isBuffering == buffering) { | ||
return; | ||
} | ||
isBuffering = buffering; | ||
if (buffering) { | ||
events.onBufferingStart(); | ||
} else { | ||
events.onBufferingEnd(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("SuspiciousNameCombination") | ||
private void sendInitialized() { | ||
if (isInitialized) { | ||
return; | ||
} | ||
isInitialized = true; | ||
VideoSize videoSize = exoPlayer.getVideoSize(); | ||
int rotationCorrection = 0; | ||
int width = videoSize.width; | ||
int height = videoSize.height; | ||
if (width != 0 && height != 0) { | ||
int rotationDegrees = videoSize.unappliedRotationDegrees; | ||
// Switch the width/height if video was taken in portrait mode | ||
if (rotationDegrees == 90 || rotationDegrees == 270) { | ||
width = videoSize.height; | ||
height = videoSize.width; | ||
} | ||
// Rotating the video with ExoPlayer does not seem to be possible with a Surface, | ||
// so inform the Flutter code that the widget needs to be rotated to prevent | ||
// upside-down playback for videos with rotationDegrees of 180 (other orientations work | ||
// correctly without correction). | ||
if (rotationDegrees == 180) { | ||
rotationCorrection = rotationDegrees; | ||
} | ||
} | ||
events.onInitialized(width, height, exoPlayer.getDuration(), rotationCorrection); | ||
} | ||
|
||
@Override | ||
public void onPlaybackStateChanged(final int playbackState) { | ||
switch (playbackState) { | ||
case Player.STATE_BUFFERING: | ||
setBuffering(true); | ||
events.onBufferingUpdate(exoPlayer.getBufferedPosition()); | ||
break; | ||
case Player.STATE_READY: | ||
sendInitialized(); | ||
break; | ||
case Player.STATE_ENDED: | ||
events.onCompleted(); | ||
break; | ||
case Player.STATE_IDLE: | ||
break; | ||
} | ||
if (playbackState != Player.STATE_BUFFERING) { | ||
setBuffering(false); | ||
} | ||
} | ||
|
||
@Override | ||
public void onPlayerError(@NonNull final PlaybackException error) { | ||
setBuffering(false); | ||
if (error.errorCode == PlaybackException.ERROR_CODE_BEHIND_LIVE_WINDOW) { | ||
// See https://exoplayer.dev/live-streaming.html#behindlivewindowexception-and-error_code_behind_live_window | ||
exoPlayer.seekToDefaultPosition(); | ||
exoPlayer.prepare(); | ||
} else { | ||
events.onError("VideoError", "Video player had error " + error, null); | ||
} | ||
} | ||
|
||
@Override | ||
public void onIsPlayingChanged(boolean isPlaying) { | ||
events.onIsPlayingStateUpdate(isPlaying); | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 a slight behavior change, right?
Before we would always invoke
videoPlayerEvents.onBufferingUpdate(exoPlayer.getBufferedPosition())
:but now we only invoke it if we pass the check at the start of the slightly modified
setBuffering
.I don't know enough about
video_player
to know if that case (we are in the case of this switchPlayer.STATE_BUFFERING
whileisBuffering
is already true) can happen/matters, so feel free to ignore if it isn't important.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.
Not only was this a good catch, but you caught a bug missed in the previous refactor.
I've fixed this and added a test to catch regressions.