Commit ac0b11ed by borrelli Committed by Oliver Woodman

Fix for #5055 - Cannot disable audio focus after enabled.

This fixes an issue where disabling audio focus handling
while audio focus is held would not release audio focus.

A new test was added for this situation.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=220316866
parent 54075ed1
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
* Fix issue with blind seeking to windows with non-zero offset in a * Fix issue with blind seeking to windows with non-zero offset in a
`ConcatenatingMediaSource` `ConcatenatingMediaSource`
([#4873](https://github.com/google/ExoPlayer/issues/4873)). ([#4873](https://github.com/google/ExoPlayer/issues/4873)).
* Fix issue where audio focus handling could not be disabled after enabling it
([#5055](https://github.com/google/ExoPlayer/issues/5055)).
* Fix issue where subtitles were positioned incorrectly if `SubtitleView` had a * Fix issue where subtitles were positioned incorrectly if `SubtitleView` had a
non-zero position offset to its parent non-zero position offset to its parent
([#4788](https://github.com/google/ExoPlayer/issues/4788)). ([#4788](https://github.com/google/ExoPlayer/issues/4788)).
......
...@@ -144,8 +144,8 @@ public final class AudioFocusManager { ...@@ -144,8 +144,8 @@ public final class AudioFocusManager {
*/ */
public @PlayerCommand int setAudioAttributes( public @PlayerCommand int setAudioAttributes(
@Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) {
if (audioAttributes == null) { if (this.audioAttributes == null && audioAttributes == null) {
return PLAYER_COMMAND_PLAY_WHEN_READY; return playWhenReady ? PLAYER_COMMAND_PLAY_WHEN_READY : PLAYER_COMMAND_DO_NOT_PLAY;
} }
Assertions.checkNotNull( Assertions.checkNotNull(
......
...@@ -58,7 +58,38 @@ public class AudioFocusManagerTest { ...@@ -58,7 +58,38 @@ public class AudioFocusManagerTest {
assertThat( assertThat(
audioFocusManager.setAudioAttributes( audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_IDLE)) /* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_IDLE))
.isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK);
assertThat(
audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ true, Player.STATE_READY))
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
ShadowAudioManager.AudioFocusRequest request =
Shadows.shadowOf(audioManager).getLastAudioFocusRequest();
assertThat(request).isNull();
}
@Test
public void setAudioAttributes_withNullUsage_releasesAudioFocus() {
// Create attributes and request audio focus.
AudioAttributes media = new AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).build();
Shadows.shadowOf(audioManager)
.setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
assertThat(
audioFocusManager.setAudioAttributes(
media, /* playWhenReady= */ true, Player.STATE_READY))
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
ShadowAudioManager.AudioFocusRequest request =
Shadows.shadowOf(audioManager).getLastAudioFocusRequest();
assertThat(request.durationHint).isEqualTo(AudioManager.AUDIOFOCUS_GAIN);
// Ensure that setting null audio attributes with audio focus releases audio focus.
assertThat(
audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ true, Player.STATE_READY))
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
AudioManager.OnAudioFocusChangeListener lastRequest =
Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener();
assertThat(lastRequest).isNotNull();
} }
@Test @Test
...@@ -296,7 +327,7 @@ public class AudioFocusManagerTest { ...@@ -296,7 +327,7 @@ public class AudioFocusManagerTest {
assertThat( assertThat(
audioFocusManager.setAudioAttributes( audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_READY)) /* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_READY))
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); .isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY);
assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull(); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull();
ShadowAudioManager.AudioFocusRequest request = ShadowAudioManager.AudioFocusRequest request =
Shadows.shadowOf(audioManager).getLastAudioFocusRequest(); Shadows.shadowOf(audioManager).getLastAudioFocusRequest();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment