Add EventListener.onPlaybackSuppressionReasonChanged

Adding this callback makes sense for completeness (we have similar callbacks
for all other playback state properties), and also to detect audio focus loss
while buffering which would currently trigger no callback because isPlaying
is still false.

Issue:#6203
PiperOrigin-RevId: 271347351
This commit is contained in:
tonihei 2019-09-26 15:41:27 +01:00 committed by Oliver Woodman
parent 60a9cf68c9
commit 004b9e8e8c
3 changed files with 16 additions and 1 deletions

View File

@ -66,6 +66,9 @@
([#6161](https://github.com/google/ExoPlayer/issues/6161)). ([#6161](https://github.com/google/ExoPlayer/issues/6161)).
* Add demo app to show how to use the Android 10 `SurfaceControl` API with * Add demo app to show how to use the Android 10 `SurfaceControl` API with
ExoPlayer ([#677](https://github.com/google/ExoPlayer/issues/677)). ExoPlayer ([#677](https://github.com/google/ExoPlayer/issues/677)).
* Add `Player.onPlaybackSuppressionReasonChanged` to allow listeners to
detect playbacks suppressions (e.g. audio focus loss) directly
([#6203](https://github.com/google/ExoPlayer/issues/6203)).
### 2.10.5 (2019-09-20) ### ### 2.10.5 (2019-09-20) ###

View File

@ -264,17 +264,21 @@ import java.util.concurrent.CopyOnWriteArrayList;
internalPlayer.setPlayWhenReady(internalPlayWhenReady); internalPlayer.setPlayWhenReady(internalPlayWhenReady);
} }
boolean playWhenReadyChanged = this.playWhenReady != playWhenReady; boolean playWhenReadyChanged = this.playWhenReady != playWhenReady;
boolean suppressionReasonChanged = this.playbackSuppressionReason != playbackSuppressionReason;
this.playWhenReady = playWhenReady; this.playWhenReady = playWhenReady;
this.playbackSuppressionReason = playbackSuppressionReason; this.playbackSuppressionReason = playbackSuppressionReason;
boolean isPlaying = isPlaying(); boolean isPlaying = isPlaying();
boolean isPlayingChanged = oldIsPlaying != isPlaying; boolean isPlayingChanged = oldIsPlaying != isPlaying;
if (playWhenReadyChanged || isPlayingChanged) { if (playWhenReadyChanged || suppressionReasonChanged || isPlayingChanged) {
int playbackState = playbackInfo.playbackState; int playbackState = playbackInfo.playbackState;
notifyListeners( notifyListeners(
listener -> { listener -> {
if (playWhenReadyChanged) { if (playWhenReadyChanged) {
listener.onPlayerStateChanged(playWhenReady, playbackState); listener.onPlayerStateChanged(playWhenReady, playbackState);
} }
if (suppressionReasonChanged) {
listener.onPlaybackSuppressionReasonChanged(playbackSuppressionReason);
}
if (isPlayingChanged) { if (isPlayingChanged) {
listener.onIsPlayingChanged(isPlaying); listener.onIsPlayingChanged(isPlaying);
} }

View File

@ -392,6 +392,14 @@ public interface Player {
*/ */
default void onPlayerStateChanged(boolean playWhenReady, @State int playbackState) {} default void onPlayerStateChanged(boolean playWhenReady, @State int playbackState) {}
/**
* Called when the value returned from {@link #getPlaybackSuppressionReason()} changes.
*
* @param playbackSuppressionReason The current {@link PlaybackSuppressionReason}.
*/
default void onPlaybackSuppressionReasonChanged(
@PlaybackSuppressionReason int playbackSuppressionReason) {}
/** /**
* Called when the value of {@link #isPlaying()} changes. * Called when the value of {@link #isPlaying()} changes.
* *