Search in sources :

Example 1 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class PlayerInfo method fromBundle.

private static PlayerInfo fromBundle(Bundle bundle) {
    @Nullable PlaybackException playerError = BundleableUtil.fromNullableBundle(PlaybackException.CREATOR, bundle.getBundle(keyForField(FIELD_PLAYBACK_ERROR)));
    int mediaItemTransitionReason = bundle.getInt(keyForField(FIELD_MEDIA_ITEM_TRANSITION_REASON), MEDIA_ITEM_TRANSITION_REASON_REPEAT);
    SessionPositionInfo sessionPositionInfo = BundleableUtil.fromNullableBundle(SessionPositionInfo.CREATOR, bundle.getBundle(keyForField(FIELD_SESSION_POSITION_INFO)), SessionPositionInfo.DEFAULT);
    PositionInfo oldPositionInfo = BundleableUtil.fromNullableBundle(PositionInfo.CREATOR, bundle.getBundle(keyForField(FIELD_OLD_POSITION_INFO)), SessionPositionInfo.DEFAULT_POSITION_INFO);
    PositionInfo newPositionInfo = BundleableUtil.fromNullableBundle(PositionInfo.CREATOR, bundle.getBundle(keyForField(FIELD_NEW_POSITION_INFO)), SessionPositionInfo.DEFAULT_POSITION_INFO);
    int discontinuityReason = bundle.getInt(keyForField(FIELD_DISCONTINUITY_REASON), DISCONTINUITY_REASON_AUTO_TRANSITION);
    @Nullable Bundle playbackParametersBundle = bundle.getBundle(keyForField(FIELD_PLAYBACK_PARAMETERS));
    PlaybackParameters playbackParameters = BundleableUtil.fromNullableBundle(PlaybackParameters.CREATOR, playbackParametersBundle, /* defaultValue= */
    PlaybackParameters.DEFAULT);
    @Player.RepeatMode int repeatMode = bundle.getInt(keyForField(FIELD_REPEAT_MODE), /* defaultValue= */
    Player.REPEAT_MODE_OFF);
    boolean shuffleModeEnabled = bundle.getBoolean(keyForField(FIELD_SHUFFLE_MODE_ENABLED), /* defaultValue= */
    false);
    Timeline timeline = BundleableUtil.fromNullableBundle(Timeline.CREATOR, bundle.getBundle(keyForField(FIELD_TIMELINE)), Timeline.EMPTY);
    VideoSize videoSize = BundleableUtil.fromNullableBundle(VideoSize.CREATOR, bundle.getBundle(keyForField(FIELD_VIDEO_SIZE)), VideoSize.UNKNOWN);
    MediaMetadata playlistMetadata = BundleableUtil.fromNullableBundle(MediaMetadata.CREATOR, bundle.getBundle(keyForField(FIELD_PLAYLIST_METADATA)), MediaMetadata.EMPTY);
    float volume = bundle.getFloat(keyForField(FIELD_VOLUME), /* defaultValue= */
    1);
    AudioAttributes audioAttributes = BundleableUtil.fromNullableBundle(AudioAttributes.CREATOR, bundle.getBundle(keyForField(FIELD_AUDIO_ATTRIBUTES)), /* defaultValue= */
    AudioAttributes.DEFAULT);
    List<Cue> cues = BundleableUtil.fromBundleNullableList(Cue.CREATOR, bundle.getParcelableArrayList(keyForField(FIELD_CUES)), /* defaultValue= */
    ImmutableList.of());
    @Nullable Bundle deviceInfoBundle = bundle.getBundle(keyForField(FIELD_DEVICE_INFO));
    DeviceInfo deviceInfo = BundleableUtil.fromNullableBundle(DeviceInfo.CREATOR, deviceInfoBundle, /* defaultValue= */
    DeviceInfo.UNKNOWN);
    int deviceVolume = bundle.getInt(keyForField(FIELD_DEVICE_VOLUME), /* defaultValue= */
    0);
    boolean deviceMuted = bundle.getBoolean(keyForField(FIELD_DEVICE_MUTED), /* defaultValue= */
    false);
    boolean playWhenReady = bundle.getBoolean(keyForField(FIELD_PLAY_WHEN_READY), /* defaultValue= */
    false);
    int playWhenReadyChangedReason = bundle.getInt(keyForField(FIELD_PLAY_WHEN_READY_CHANGED_REASON), /* defaultValue= */
    PLAY_WHEN_READY_CHANGE_REASON_USER_REQUEST);
    @Player.PlaybackSuppressionReason int playbackSuppressionReason = bundle.getInt(keyForField(FIELD_PLAYBACK_SUPPRESSION_REASON), /* defaultValue= */
    PLAYBACK_SUPPRESSION_REASON_NONE);
    @Player.State int playbackState = bundle.getInt(keyForField(FIELD_PLAYBACK_STATE), /* defaultValue= */
    STATE_IDLE);
    boolean isPlaying = bundle.getBoolean(keyForField(FIELD_IS_PLAYING), /* defaultValue= */
    false);
    boolean isLoading = bundle.getBoolean(keyForField(FIELD_IS_LOADING), /* defaultValue= */
    false);
    MediaMetadata mediaMetadata = BundleableUtil.fromNullableBundle(MediaMetadata.CREATOR, bundle.getBundle(keyForField(FIELD_MEDIA_METADATA)), MediaMetadata.EMPTY);
    long seekBackIncrementMs = bundle.getLong(keyForField(FIELD_SEEK_BACK_INCREMENT_MS), /* defaultValue= */
    0);
    long seekForwardIncrementMs = bundle.getLong(keyForField(FIELD_SEEK_FORWARD_INCREMENT_MS), /* defaultValue= */
    0);
    long maxSeekToPreviousPosition = bundle.getLong(keyForField(FIELD_MAX_SEEK_TO_PREVIOUS_POSITION_MS), /* defaultValue= */
    0);
    TrackSelectionParameters trackSelectionParameters = BundleableUtil.fromNullableBundle(TrackSelectionParameters.CREATOR, bundle.getBundle(keyForField(FIELD_TRACK_SELECTION_PARAMETERS)), TrackSelectionParameters.DEFAULT_WITHOUT_CONTEXT);
    return new PlayerInfo(playerError, mediaItemTransitionReason, sessionPositionInfo, oldPositionInfo, newPositionInfo, discontinuityReason, playbackParameters, repeatMode, shuffleModeEnabled, videoSize, timeline, playlistMetadata, volume, audioAttributes, cues, deviceInfo, deviceVolume, deviceMuted, playWhenReady, playWhenReadyChangedReason, playbackSuppressionReason, playbackState, isPlaying, isLoading, mediaMetadata, seekBackIncrementMs, seekForwardIncrementMs, maxSeekToPreviousPosition, trackSelectionParameters);
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) PlaybackSuppressionReason(androidx.media3.common.Player.PlaybackSuppressionReason) Bundle(android.os.Bundle) AudioAttributes(androidx.media3.common.AudioAttributes) State(androidx.media3.common.Player.State) Timeline(androidx.media3.common.Timeline) Cue(androidx.media3.common.text.Cue) TrackSelectionParameters(androidx.media3.common.TrackSelectionParameters) VideoSize(androidx.media3.common.VideoSize) DeviceInfo(androidx.media3.common.DeviceInfo) PositionInfo(androidx.media3.common.Player.PositionInfo) MediaMetadata(androidx.media3.common.MediaMetadata) Nullable(androidx.annotation.Nullable) PlaybackParameters(androidx.media3.common.PlaybackParameters)

Example 2 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaControllerImplLegacy method buildNewControllerInfo.

private static ControllerInfo buildNewControllerInfo(boolean initialUpdate, LegacyPlayerInfo oldLegacyPlayerInfo, ControllerInfo oldControllerInfo, LegacyPlayerInfo newLegacyPlayerInfo, long sessionFlags, boolean isSessionReady, @RatingCompat.Style int ratingType, long timeDiffMs) {
    QueueTimeline currentTimeline;
    MediaMetadata mediaMetadata;
    int currentMediaItemIndex;
    MediaMetadata playlistMetadata;
    @RepeatMode int repeatMode;
    boolean shuffleModeEnabled;
    SessionCommands availableSessionCommands;
    Commands availablePlayerCommands;
    ImmutableList<CommandButton> customLayout;
    boolean isQueueChanged = oldLegacyPlayerInfo.queue != newLegacyPlayerInfo.queue;
    currentTimeline = isQueueChanged ? QueueTimeline.create(newLegacyPlayerInfo.queue) : new QueueTimeline((QueueTimeline) oldControllerInfo.playerInfo.timeline);
    boolean isMetadataCompatChanged = oldLegacyPlayerInfo.mediaMetadataCompat != newLegacyPlayerInfo.mediaMetadataCompat || initialUpdate;
    long oldActiveQueueId = getActiveQueueId(oldLegacyPlayerInfo.playbackStateCompat);
    long newActiveQueueId = getActiveQueueId(newLegacyPlayerInfo.playbackStateCompat);
    boolean isCurrentActiveQueueIdChanged = (oldActiveQueueId != newActiveQueueId) || initialUpdate;
    if (isMetadataCompatChanged || isCurrentActiveQueueIdChanged || isQueueChanged) {
        currentMediaItemIndex = findQueueItemIndex(newLegacyPlayerInfo.queue, newActiveQueueId);
        boolean hasMediaMetadataCompat = newLegacyPlayerInfo.mediaMetadataCompat != null;
        if (hasMediaMetadataCompat && isMetadataCompatChanged) {
            mediaMetadata = MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
        } else if (!hasMediaMetadataCompat && isCurrentActiveQueueIdChanged) {
            mediaMetadata = (currentMediaItemIndex == C.INDEX_UNSET) ? MediaMetadata.EMPTY : MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.queue.get(currentMediaItemIndex).getDescription(), ratingType);
        } else {
            mediaMetadata = oldControllerInfo.playerInfo.mediaMetadata;
        }
        if (currentMediaItemIndex == C.INDEX_UNSET && isMetadataCompatChanged) {
            if (hasMediaMetadataCompat) {
                Log.w(TAG, "Adding a fake MediaItem at the end of the list because there's no QueueItem with" + " the active queue id and current Timeline should have currently playing" + " MediaItem.");
                MediaItem fakeMediaItem = MediaUtils.convertToMediaItem(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
                currentTimeline = currentTimeline.copyWithFakeMediaItem(fakeMediaItem);
                currentMediaItemIndex = currentTimeline.getWindowCount() - 1;
            } else {
                currentTimeline = currentTimeline.copyWithFakeMediaItem(/* fakeMediaItem= */
                null);
                // Shouldn't be C.INDEX_UNSET to make getCurrentMediaItemIndex() return masked index.
                // In other words, this index is either the currently playing media item index or the
                // would-be playing index when playing.
                currentMediaItemIndex = 0;
            }
        } else if (currentMediaItemIndex != C.INDEX_UNSET) {
            currentTimeline = currentTimeline.copyWithFakeMediaItem(/* fakeMediaItem= */
            null);
            if (hasMediaMetadataCompat) {
                MediaItem mediaItem = MediaUtils.convertToMediaItem(currentTimeline.getMediaItemAt(currentMediaItemIndex).mediaId, newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
                currentTimeline = currentTimeline.copyWithNewMediaItem(/* replaceIndex= */
                currentMediaItemIndex, mediaItem);
            }
        } else {
            // There's queue, but no valid queue item ID nor current media item metadata.
            // Fallback to use 0 as current media item index to mask current item index.
            currentMediaItemIndex = 0;
        }
    } else {
        currentMediaItemIndex = oldControllerInfo.playerInfo.sessionPositionInfo.positionInfo.mediaItemIndex;
        mediaMetadata = oldControllerInfo.playerInfo.mediaMetadata;
    }
    playlistMetadata = oldLegacyPlayerInfo.queueTitle == newLegacyPlayerInfo.queueTitle ? oldControllerInfo.playerInfo.playlistMetadata : MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.queueTitle);
    repeatMode = MediaUtils.convertToRepeatMode(newLegacyPlayerInfo.repeatMode);
    shuffleModeEnabled = MediaUtils.convertToShuffleModeEnabled(newLegacyPlayerInfo.shuffleMode);
    if (oldLegacyPlayerInfo.playbackStateCompat != newLegacyPlayerInfo.playbackStateCompat) {
        availableSessionCommands = MediaUtils.convertToSessionCommands(newLegacyPlayerInfo.playbackStateCompat, isSessionReady);
        customLayout = MediaUtils.convertToCustomLayout(newLegacyPlayerInfo.playbackStateCompat);
    } else {
        availableSessionCommands = oldControllerInfo.availableSessionCommands;
        customLayout = oldControllerInfo.customLayout;
    }
    // Note: Sets the available player command here although it can be obtained before session is
    // ready. It's to follow the decision on MediaController to disallow any commands before
    // connection is made.
    availablePlayerCommands = (oldControllerInfo.availablePlayerCommands == Commands.EMPTY) ? MediaUtils.convertToPlayerCommands(sessionFlags, isSessionReady) : oldControllerInfo.availablePlayerCommands;
    PlaybackException playerError = MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
    long durationMs = MediaUtils.convertToDurationMs(newLegacyPlayerInfo.mediaMetadataCompat);
    long currentPositionMs = MediaUtils.convertToCurrentPositionMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    long bufferedPositionMs = MediaUtils.convertToBufferedPositionMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    int bufferedPercentage = MediaUtils.convertToBufferedPercentage(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    long totalBufferedDurationMs = MediaUtils.convertToTotalBufferedDurationMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    boolean isPlayingAd = MediaUtils.convertToIsPlayingAd(newLegacyPlayerInfo.mediaMetadataCompat);
    PlaybackParameters playbackParameters = MediaUtils.convertToPlaybackParameters(newLegacyPlayerInfo.playbackStateCompat);
    AudioAttributes audioAttributes = MediaUtils.convertToAudioAttributes(newLegacyPlayerInfo.playbackInfoCompat);
    boolean playWhenReady = MediaUtils.convertToPlayWhenReady(newLegacyPlayerInfo.playbackStateCompat);
    @State int playbackState = MediaUtils.convertToPlaybackState(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    boolean isPlaying = MediaUtils.convertToIsPlaying(newLegacyPlayerInfo.playbackStateCompat);
    DeviceInfo deviceInfo = MediaUtils.convertToDeviceInfo(newLegacyPlayerInfo.playbackInfoCompat);
    int deviceVolume = MediaUtils.convertToDeviceVolume(newLegacyPlayerInfo.playbackInfoCompat);
    boolean deviceMuted = MediaUtils.convertToIsDeviceMuted(newLegacyPlayerInfo.playbackInfoCompat);
    long seekBackIncrementMs = oldControllerInfo.playerInfo.seekBackIncrementMs;
    long seekForwardIncrementMs = oldControllerInfo.playerInfo.seekForwardIncrementMs;
    return createControllerInfo(currentTimeline, mediaMetadata, currentMediaItemIndex, playlistMetadata, repeatMode, shuffleModeEnabled, availableSessionCommands, availablePlayerCommands, customLayout, playerError, durationMs, currentPositionMs, bufferedPositionMs, bufferedPercentage, totalBufferedDurationMs, isPlayingAd, playbackParameters, audioAttributes, playWhenReady, playbackState, isPlaying, deviceInfo, deviceVolume, deviceMuted, seekBackIncrementMs, seekForwardIncrementMs);
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) AudioAttributes(androidx.media3.common.AudioAttributes) RepeatMode(androidx.media3.common.Player.RepeatMode) MediaItem(androidx.media3.common.MediaItem) State(androidx.media3.common.Player.State) Commands(androidx.media3.common.Player.Commands) DeviceInfo(androidx.media3.common.DeviceInfo) MediaMetadata(androidx.media3.common.MediaMetadata) PlaybackParameters(androidx.media3.common.PlaybackParameters)

Example 3 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaControllerImplLegacy method createControllerInfo.

private static ControllerInfo createControllerInfo(QueueTimeline currentTimeline, MediaMetadata mediaMetadata, int currentMediaItemIndex, MediaMetadata playlistMetadata, @RepeatMode int repeatMode, boolean shuffleModeEnabled, SessionCommands availableSessionCommands, Commands availablePlayerCommands, ImmutableList<CommandButton> customLayout, @Nullable PlaybackException playerError, long durationMs, long currentPositionMs, long bufferedPositionMs, int bufferedPercentage, long totalBufferedDurationMs, boolean isPlayingAd, PlaybackParameters playbackParameters, AudioAttributes audioAttributes, boolean playWhenReady, int playbackState, boolean isPlaying, DeviceInfo deviceInfo, int deviceVolume, boolean deviceMuted, long seekBackIncrementMs, long seekForwardIncrementMs) {
    @Nullable MediaItem currentMediaItem = currentTimeline.getMediaItemAt(currentMediaItemIndex);
    PositionInfo positionInfo = createPositionInfo(currentMediaItemIndex, currentMediaItem, currentPositionMs);
    SessionPositionInfo sessionPositionInfo = new SessionPositionInfo(/* positionInfo= */
    positionInfo, /* isPlayingAd= */
    isPlayingAd, /* eventTimeMs= */
    C.TIME_UNSET, /* durationMs= */
    durationMs, /* bufferedPositionMs= */
    bufferedPositionMs, /* bufferedPercentage= */
    bufferedPercentage, /* totalBufferedDurationMs= */
    totalBufferedDurationMs, /* currentLiveOffsetMs= */
    C.TIME_UNSET, /* contentDurationMs= */
    durationMs, /* contentBufferedPositionMs= */
    bufferedPositionMs);
    PlayerInfo playerInfo = new PlayerInfo(/* playerError= */
    playerError, /* mediaItemTransitionReason= */
    PlayerInfo.MEDIA_ITEM_TRANSITION_REASON_DEFAULT, /* sessionPositionInfo= */
    sessionPositionInfo, /* oldPositionInfo= */
    SessionPositionInfo.DEFAULT_POSITION_INFO, /* newPositionInfo= */
    SessionPositionInfo.DEFAULT_POSITION_INFO, /* discontinuityReason= */
    PlayerInfo.DISCONTINUITY_REASON_DEFAULT, /* playbackParameters= */
    playbackParameters, /* repeatMode= */
    repeatMode, /* shuffleModeEnabled= */
    shuffleModeEnabled, /* videoSize= */
    VideoSize.UNKNOWN, /* timeline= */
    currentTimeline, /* playlistMetadata= */
    playlistMetadata, /* volume= */
    1.0f, /* audioAttributes= */
    audioAttributes, /* cues= */
    Collections.emptyList(), /* deviceInfo= */
    deviceInfo, /* deviceVolume= */
    deviceVolume, /* deviceMuted= */
    deviceMuted, /* playWhenReady= */
    playWhenReady, /* playWhenReadyChangedReason= */
    PlayerInfo.PLAY_WHEN_READY_CHANGE_REASON_DEFAULT, /* playbackSuppressionReason= */
    Player.PLAYBACK_SUPPRESSION_REASON_NONE, /* playbackState= */
    playbackState, /* isPlaying= */
    isPlaying, /* isLoading= */
    false, /* mediaMetadata= */
    mediaMetadata, seekBackIncrementMs, seekForwardIncrementMs, /* maxSeekToPreviousPositionMs= */
    0L, /* parameters= */
    TrackSelectionParameters.DEFAULT_WITHOUT_CONTEXT);
    return new ControllerInfo(playerInfo, availableSessionCommands, availablePlayerCommands, customLayout);
}
Also used : MediaItem(androidx.media3.common.MediaItem) PositionInfo(androidx.media3.common.Player.PositionInfo) Nullable(androidx.annotation.Nullable)

Example 4 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_playbackTypeNotChanged_local.

@Test
public void setPlayer_playbackTypeNotChanged_local() throws Exception {
    DeviceInfo deviceInfo = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_LOCAL, /* minVolume= */
    0, /* maxVolume= */
    10);
    int legacyPlaybackType = MediaControllerCompat.PlaybackInfo.PLAYBACK_TYPE_LOCAL;
    int legacyStream = AudioManager.STREAM_RING;
    AudioAttributesCompat attrsCompat = new AudioAttributesCompat.Builder().setLegacyStreamType(legacyStream).build();
    AudioAttributes attrs = MediaUtils.convertToAudioAttributes(attrsCompat);
    CountDownLatch playbackInfoNotified = new CountDownLatch(1);
    MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {

        @Override
        public void onAudioInfoChanged(MediaControllerCompat.PlaybackInfo info) {
            if (info.getPlaybackType() == legacyPlaybackType && info.getAudioAttributes().getLegacyStreamType() == legacyStream) {
                playbackInfoNotified.countDown();
            }
        }
    };
    controllerCompat.registerCallback(callback, handler);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfo).setAudioAttributes(attrs).build();
    session.setPlayer(playerConfig);
    // In API 21+, onAudioInfoChanged() is not called when playbackType is not changed.
    if (Util.SDK_INT >= 21) {
        PollingCheck.waitFor(TIMEOUT_MS, () -> {
            MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
            return info.getPlaybackType() == legacyPlaybackType && info.getAudioAttributes().getLegacyStreamType() == legacyStream;
        });
    } else {
        assertThat(playbackInfoNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
        MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
        assertThat(info.getPlaybackType()).isEqualTo(legacyPlaybackType);
        assertThat(info.getAudioAttributes().getLegacyStreamType()).isEqualTo(legacyStream);
    }
}
Also used : AudioAttributesCompat(androidx.media.AudioAttributesCompat) Bundle(android.os.Bundle) AudioAttributes(androidx.media3.common.AudioAttributes) CountDownLatch(java.util.concurrent.CountDownLatch) DeviceInfo(androidx.media3.common.DeviceInfo) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 5 with AudioAttributes

use of androidx.media3.common.AudioAttributes in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method setVolumeWithLocalVolume.

@Test
public void setVolumeWithLocalVolume() throws Exception {
    if (Util.SDK_INT >= 21 && audioManager.isVolumeFixed()) {
        // This test is not eligible for this device.
        return;
    }
    session = new MediaSession.Builder(context, player).setId("setVolumeWithLocalVolume").setSessionCallback(new TestSessionCallback()).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    // Here, we intentionally choose STREAM_ALARM in order not to consider
    // 'Do Not Disturb' or 'Volume limit'.
    int stream = AudioManager.STREAM_ALARM;
    int maxVolume = AudioManagerCompat.getStreamMaxVolume(audioManager, stream);
    int minVolume = AudioManagerCompat.getStreamMinVolume(audioManager, stream);
    Log.d(TAG, "maxVolume=" + maxVolume + ", minVolume=" + minVolume);
    if (maxVolume <= minVolume) {
        return;
    }
    handler.postAndSync(() -> {
        // Set stream of the session.
        AudioAttributes attrs = MediaUtils.convertToAudioAttributes(new AudioAttributesCompat.Builder().setLegacyStreamType(stream).build());
        player.audioAttributes = attrs;
        player.notifyAudioAttributesChanged(attrs);
    });
    int originalVolume = audioManager.getStreamVolume(stream);
    int targetVolume = originalVolume == minVolume ? originalVolume + 1 : originalVolume - 1;
    Log.d(TAG, "originalVolume=" + originalVolume + ", targetVolume=" + targetVolume);
    controller.setVolumeTo(targetVolume, AudioManager.FLAG_SHOW_UI);
    PollingCheck.waitFor(VOLUME_CHANGE_TIMEOUT_MS, () -> targetVolume == audioManager.getStreamVolume(stream));
    // Set back to original volume.
    audioManager.setStreamVolume(stream, originalVolume, /* flags= */
    0);
}
Also used : AudioAttributesCompat(androidx.media.AudioAttributesCompat) AudioAttributes(androidx.media3.common.AudioAttributes) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

AudioAttributes (androidx.media3.common.AudioAttributes)15 Test (org.junit.Test)11 LargeTest (androidx.test.filters.LargeTest)7 AudioAttributesCompat (androidx.media.AudioAttributesCompat)6 Bundle (android.os.Bundle)5 DeviceInfo (androidx.media3.common.DeviceInfo)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 Nullable (androidx.annotation.Nullable)3 MediaMetadata (androidx.media3.common.MediaMetadata)3 PositionInfo (androidx.media3.common.Player.PositionInfo)3 State (androidx.media3.common.Player.State)3 AuxEffectInfo (androidx.media3.common.AuxEffectInfo)2 MediaItem (androidx.media3.common.MediaItem)2 PlaybackException (androidx.media3.common.PlaybackException)2 PlaybackParameters (androidx.media3.common.PlaybackParameters)2 RepeatMode (androidx.media3.common.Player.RepeatMode)2 Timeline (androidx.media3.common.Timeline)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SuppressLint (android.annotation.SuppressLint)1