Fix audio focus

This commit is contained in:
Oliver Woodman 2018-11-07 19:24:02 +00:00
parent f5c3b30290
commit a02a75ba57
2 changed files with 12 additions and 12 deletions

View File

@ -163,11 +163,9 @@ public final class AudioFocusManager {
}
}
if (playerState == Player.STATE_IDLE) {
return PLAYER_COMMAND_WAIT_FOR_CALLBACK;
} else {
return handlePrepare(playWhenReady);
}
return playerState == Player.STATE_IDLE
? handleIdle(playWhenReady)
: handlePrepare(playWhenReady);
}
/**
@ -199,12 +197,9 @@ public final class AudioFocusManager {
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)}. */
@ -218,6 +213,11 @@ public final class AudioFocusManager {
// 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;

View File

@ -58,7 +58,7 @@ public class AudioFocusManagerTest {
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))
@ -148,7 +148,7 @@ public class AudioFocusManagerTest {
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);