/** * Sets the stream type for audio playback, used by the underlying audio track. * <p> * Setting the stream type during playback may introduce a short gap in audio output as the audio * track is recreated. A new audio session id will also be generated. * <p> * Calling this method overwrites any attributes set previously by calling * {@link #setAudioAttributes(AudioAttributes)}. * * @deprecated Use {@link #setAudioAttributes(AudioAttributes)}. * @param streamType The stream type for audio playback. */ @Deprecated public void setAudioStreamType(@C.StreamType int streamType) { @C.AudioUsage int usage = Util.getAudioUsageForStreamType(streamType); @C.AudioContentType int contentType = Util.getAudioContentTypeForStreamType(streamType); AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(usage).setContentType(contentType).build(); setAudioAttributes(audioAttributes); }
@Test public void onAudioFocusChange_withPausedWhenDucked_sendsCommandWaitForCallback() { // Ensure that the player is commanded to pause when audio focus is lost with // AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK and the content type is CONTENT_TYPE_SPEECH. AudioAttributes media = new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_SPEECH) .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(); request.listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK); assertThat(testPlayerControl.lastPlayerCommand).isEqualTo(PLAYER_COMMAND_WAIT_FOR_CALLBACK); assertThat(testPlayerControl.lastVolumeMultiplier).isEqualTo(1.0f); request.listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_GAIN); assertThat(testPlayerControl.lastPlayerCommand).isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); }
@Test public void handleStop_withoutAudioFocus_stillAbandonsFocus() { // Ensure that handleStop causes AudioFocusManager to call through to abandon audio focus // even if focus wasn't requested. AudioAttributes media = new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_SPEECH) .build(); Shadows.shadowOf(audioManager) .setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED); assertThat( audioFocusManager.setAudioAttributes( media, /* playWhenReady= */ false, Player.STATE_READY)) .isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull(); ShadowAudioManager.AudioFocusRequest request = Shadows.shadowOf(audioManager).getLastAudioFocusRequest(); assertThat(request).isNull(); audioFocusManager.handleStop(); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNotNull(); }
.setContentType(C.CONTENT_TYPE_MOVIE) .build(); player.setAudioAttributes(audioAttributes,true);
.setContentType(CONTENT_TYPE_MUSIC) .setUsage(USAGE_MEDIA) .build();
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioManager.STREAM_MUSIC).build());
.setContentType(C.CONTENT_TYPE_MOVIE) .build(); player.setAudioAttributes(audioAttributes, true);
@Override protected void onPostExecute(Integer connectionType) { if (connectionType == CONNECTION_TYPE_ERROR) { Toast.makeText(PlayerService.this, getString(R.string.toastalert_unable_to_connect), Toast.LENGTH_LONG).show(); stopPlayback(false); } else if (mStation.getPlaybackState() != PLAYBACK_STATE_STOPPED) { // prepare player preparePlayer(connectionType); // add listener mPlayer.addListener(PlayerService.this); // set content type mPlayer.setAudioAttributes(new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_MUSIC) .build() ); } // release init lock mPlayerInitLock = false; }
private void setMovieAudioAttributes(SimpleExoPlayer exoPlayer) { if (androidDeviceVersion.isLollipopTwentyOneOrAbove()) { AudioAttributes audioAttributes = new AudioAttributes.Builder() .setContentType(C.CONTENT_TYPE_MOVIE) .build(); exoPlayer.setAudioAttributes(audioAttributes); } }
@Test public void givenLollipopDevice_whenLoadingVideo_thenSetsMovieAudioAttributesOnExoPlayer() { given(androidDeviceVersion.isLollipopTwentyOneOrAbove()).willReturn(true); facade.loadVideo(surfaceViewHolder, drmSessionCreator, uri, OPTIONS, exoPlayerForwarder, mediaCodecSelector); AudioAttributes expectedMovieAudioAttributes = new AudioAttributes.Builder() .setContentType(C.CONTENT_TYPE_MOVIE) .build(); verify(exoPlayer).setAudioAttributes(expectedMovieAudioAttributes); }
public void setAudioStreamType(int streamType) { @C.AudioUsage int usage = Util.getAudioUsageForStreamType(streamType); @C.AudioContentType int contentType = Util.getAudioContentTypeForStreamType(streamType); AudioAttributes audioAttributes = new AudioAttributes.Builder() .setUsage(usage) .setContentType(contentType) .build(); sendMessage(C.TRACK_TYPE_AUDIO, C.MSG_SET_AUDIO_ATTRIBUTES, audioAttributes); }
@Test public void onAudioFocusChange_withAudioFocusLost_sendsDoNotPlayAndAbandondsFocus() { // Ensure that AUDIOFOCUS_LOSS causes AudioFocusManager to pause playback and abandon audio // focus. AudioAttributes media = new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_SPEECH) .build(); Shadows.shadowOf(audioManager) .setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED); assertThat( audioFocusManager.setAudioAttributes( media, /* playWhenReady= */ true, Player.STATE_READY)) .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull(); ShadowAudioManager.AudioFocusRequest request = Shadows.shadowOf(audioManager).getLastAudioFocusRequest(); request.listener.onAudioFocusChange(AudioManager.AUDIOFOCUS_LOSS); assertThat(testPlayerControl.lastPlayerCommand).isEqualTo(PLAYER_COMMAND_DO_NOT_PLAY); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()) .isEqualTo(request.listener); }
@Test public void handleStop_withAudioFocus_abandonsAudioFocus() { // Ensure that handleStop causes AudioFocusManager to abandon audio focus. AudioAttributes media = new AudioAttributes.Builder() .setUsage(C.USAGE_MEDIA) .setContentType(C.CONTENT_TYPE_SPEECH) .build(); Shadows.shadowOf(audioManager) .setNextFocusRequestResponse(AudioManager.AUDIOFOCUS_REQUEST_GRANTED); assertThat( audioFocusManager.setAudioAttributes( media, /* playWhenReady= */ true, Player.STATE_READY)) .isEqualTo(PLAYER_COMMAND_PLAY_WHEN_READY); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()).isNull(); ShadowAudioManager.AudioFocusRequest request = Shadows.shadowOf(audioManager).getLastAudioFocusRequest(); audioFocusManager.handleStop(); assertThat(Shadows.shadowOf(audioManager).getLastAbandonedAudioFocusListener()) .isEqualTo(request.listener); }