Skip to content

Commit

Permalink
Fix for #5055 - Cannot disable audio focus after enabled.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nic0lette authored and ojw28 committed Nov 7, 2018
1 parent abb7d8a commit 0c4d0ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### dev-v2 (not yet released) ###

* Fix issue where audio focus handling could not be disabled after enabling
it ([#5055](https://github.com/google/ExoPlayer/issues/5055)).
* Support for playing spherical videos on Daydream.
* Improve decoder re-use between playbacks. TODO: Write and link a blog post
here ([#2826](https://github.com/google/ExoPlayer/issues/2826)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public float getVolumeMultiplier() {
*/
public @PlayerCommand int setAudioAttributes(
@Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) {
if (audioAttributes == null) {
return PLAYER_COMMAND_PLAY_WHEN_READY;
}

if (!Util.areEqual(this.audioAttributes, audioAttributes)) {
this.audioAttributes = audioAttributes;
focusGain = convertAudioAttributesToFocusGain(audioAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,38 @@ public void setAudioAttributes_withNullUsage_doesNotManageAudioFocus() {
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
Expand Down Expand Up @@ -296,7 +327,7 @@ public void handleStop_withoutHandlingAudioFocus_isNoOp() {
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();
Expand Down

0 comments on commit 0c4d0ee

Please sign in to comment.