Skip to content

Commit

Permalink
Fix audio focus
Browse files Browse the repository at this point in the history
  • Loading branch information
ojw28 committed Nov 7, 2018
1 parent f5c3b30 commit a02a75b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,9 @@ public float getVolumeMultiplier() {
}
}

if (playerState == Player.STATE_IDLE) {
return PLAYER_COMMAND_WAIT_FOR_CALLBACK;
} else {
return handlePrepare(playWhenReady);
}
return playerState == Player.STATE_IDLE
? handleIdle(playWhenReady)
: handlePrepare(playWhenReady);
}

/**
Expand Down Expand Up @@ -199,12 +197,9 @@ public float getVolumeMultiplier() {
if (!playWhenReady) {
abandonAudioFocus();
return PLAYER_COMMAND_DO_NOT_PLAY;
} else if (playerState != Player.STATE_IDLE) {
return requestAudioFocus();
}
return focusGain != C.AUDIOFOCUS_NONE
? PLAYER_COMMAND_WAIT_FOR_CALLBACK
: PLAYER_COMMAND_PLAY_WHEN_READY;

return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : requestAudioFocus();
}

/** Called by the player as part of {@link ExoPlayer#stop(boolean)}. */
Expand All @@ -218,6 +213,11 @@ public void handleStop() {

// Internal methods.

@PlayerCommand
private int handleIdle(boolean playWhenReady) {
return playWhenReady ? PLAYER_COMMAND_PLAY_WHEN_READY : PLAYER_COMMAND_DO_NOT_PLAY;
}

private @PlayerCommand int requestAudioFocus() {
int focusRequestResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void setAudioAttributes_withNullUsage_doesNotManageAudioFocus() {
assertThat(
audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ false, Player.STATE_IDLE))
.isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK);
.isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY);
assertThat(
audioFocusManager.setAudioAttributes(
/* audioAttributes= */ null, /* playWhenReady= */ true, Player.STATE_READY))
Expand Down Expand Up @@ -148,7 +148,7 @@ public void handlePrepare_afterSetAudioAttributes_setsPlayerCommandPlayWhenReady
assertThat(
audioFocusManager.setAudioAttributes(
media, /* playWhenReady= */ true, Player.STATE_IDLE))
.isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK);
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
assertThat(Shadows.shadowOf(audioManager).getLastAudioFocusRequest()).isNull();
assertThat(audioFocusManager.handlePrepare(/* playWhenReady= */ true))
.isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY);
Expand Down

0 comments on commit a02a75b

Please sign in to comment.