@Test public void setAudioAttributes_inStateEnded_requestsAudioFocus() { AudioAttributes media = new AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).build(); Shadows.shadowOf(audioManager) .setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED); assertThat( audioFocusManager.setAudioAttributes( media, true, Player.STATE_ENDED)) .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); ShadowAudioManager.AudioFocusRequest request = Shadows.shadowOf(audioManager).getLastAudioFocusRequest(); assertThat(getAudioFocusGainFromRequest(request)).isEqualTo(AudioManager.AUDIOFOCUS_GAIN); }
@PlayerCommand public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; focusGain = convertAudioAttributesToFocusGain(audioAttributes); Assertions.checkArgument( focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME."); if (playWhenReady && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) { return requestAudioFocus(); } } return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : handlePrepare(playWhenReady); }
AudioFocusManager { @PlayerCommand public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; focusGain = convertAudioAttributesToFocusGain(audioAttributes); Assertions.checkArgument( focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME."); if (playWhenReady && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) { return requestAudioFocus(); } } return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : handlePrepare(playWhenReady); } }
AudioFocusManager { @PlayerCommand public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; focusGain = convertAudioAttributesToFocusGain(audioAttributes); Assertions.checkArgument( focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME."); if (playWhenReady && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) { return requestAudioFocus(); } } return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : handlePrepare(playWhenReady); } AudioFocusManager(Context context, PlayerControl playerControl); }
AudioFocusManager { @PlayerCommand public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; focusGain = convertAudioAttributesToFocusGain(audioAttributes); Assertions.checkArgument( focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME."); if (playWhenReady && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) { return requestAudioFocus(); } } return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : handlePrepare(playWhenReady); } AudioFocusManager(Context context, PlayerControl playerControl); float getVolumeMultiplier(); @PlayerCommand int setAudioAttributes(
      @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState); @PlayerCommand int handlePrepare(boolean playWhenReady); @PlayerCommand int handleSetPlayWhenReady(boolean playWhenReady, int playerState); void handleStop(); }
AudioFocusManager { @PlayerCommand public int setAudioAttributes( @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState) { if (!Util.areEqual(this.audioAttributes, audioAttributes)) { this.audioAttributes = audioAttributes; focusGain = convertAudioAttributesToFocusGain(audioAttributes); Assertions.checkArgument( focusGain == C.AUDIOFOCUS_GAIN || focusGain == C.AUDIOFOCUS_NONE, "Automatic handling of audio focus is only available for USAGE_MEDIA and USAGE_GAME."); if (playWhenReady && (playerState == Player.STATE_BUFFERING || playerState == Player.STATE_READY)) { return requestAudioFocus(); } } return playerState == Player.STATE_IDLE ? handleIdle(playWhenReady) : handlePrepare(playWhenReady); } AudioFocusManager(Context context, PlayerControl playerControl); float getVolumeMultiplier(); @PlayerCommand int setAudioAttributes(
      @Nullable AudioAttributes audioAttributes, boolean playWhenReady, int playerState); @PlayerCommand int handlePrepare(boolean playWhenReady); @PlayerCommand int handleSetPlayWhenReady(boolean playWhenReady, int playerState); void handleStop(); static final int PLAYER_COMMAND_DO_NOT_PLAY; static final int PLAYER_COMMAND_WAIT_FOR_CALLBACK; static final int PLAYER_COMMAND_PLAY_WHEN_READY; }