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
This commit is contained in:
borrelli 2018-11-06 11:18:28 -08:00 committed by Oliver Woodman
parent 54075ed166
commit ac0b11edc0
3 changed files with 36 additions and 3 deletions

View file

@ -29,6 +29,8 @@
* Fix issue with blind seeking to windows with non-zero offset in a
`ConcatenatingMediaSource`
([#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
non-zero position offset to its parent
([#4788](https://github.com/google/ExoPlayer/issues/4788)).

View file

@ -144,8 +144,8 @@ public final class AudioFocusManager {
*/
public @PlayerCommand int setAudioAttributes(
@Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) {
if (audioAttributes == null) {
return PLAYER_COMMAND_PLAY_WHEN_READY;
if (this.audioAttributes == null && audioAttributes == null) {
return playWhenReady ? PLAYER_COMMAND_PLAY_WHEN_READY : PLAYER_COMMAND_DO_NOT_PLAY;
}
Assertions.checkNotNull(

View file

@ -58,7 +58,38 @@ public class AudioFocusManagerTest {
assertThat(
audioFocusManager.setAudioAttributes(
/* 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);
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
@ -296,7 +327,7 @@ public class AudioFocusManagerTest {
assertThat(
audioFocusManager.setAudioAttributes(
/* 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();
ShadowAudioManager.AudioFocusRequest request =
Shadows.shadowOf(audioManager).getLastAudioFocusRequest();