use of androidx.media3.common.AudioAttributes in project media by androidx.
the class MediaControllerListenerTest method setPlayer_notifiesChangedValues.
@Test
public void setPlayer_notifiesChangedValues() throws Exception {
@State int testState = STATE_BUFFERING;
Timeline testTimeline = MediaTestUtils.createTimeline(/* windowCount= */
3);
MediaMetadata testPlaylistMetadata = new MediaMetadata.Builder().setTitle("title").build();
AudioAttributes testAudioAttributes = MediaUtils.convertToAudioAttributes(new AudioAttributesCompat.Builder().setLegacyStreamType(AudioManager.STREAM_RING).build());
boolean testShuffleModeEnabled = true;
@RepeatMode int testRepeatMode = REPEAT_MODE_ALL;
int testCurrentAdGroupIndex = 33;
int testCurrentAdIndexInAdGroup = 11;
AtomicInteger stateRef = new AtomicInteger();
AtomicReference<Timeline> timelineRef = new AtomicReference<>();
AtomicReference<MediaMetadata> playlistMetadataRef = new AtomicReference<>();
AtomicReference<AudioAttributes> audioAttributesRef = new AtomicReference<>();
AtomicInteger currentAdGroupIndexRef = new AtomicInteger();
AtomicInteger currentAdIndexInAdGroupRef = new AtomicInteger();
AtomicBoolean shuffleModeEnabledRef = new AtomicBoolean();
AtomicInteger repeatModeRef = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(7);
controller = controllerTestRule.createController(remoteSession.getToken());
threadTestRule.getHandler().postAndSync(() -> controller.addListener(new Player.Listener() {
@Override
public void onAudioAttributesChanged(AudioAttributes attributes) {
audioAttributesRef.set(attributes);
latch.countDown();
}
@Override
public void onPlaybackStateChanged(@State int playbackState) {
stateRef.set(playbackState);
latch.countDown();
}
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
timelineRef.set(timeline);
latch.countDown();
}
@Override
public void onPlaylistMetadataChanged(MediaMetadata playlistMetadata) {
playlistMetadataRef.set(playlistMetadata);
latch.countDown();
}
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
currentAdGroupIndexRef.set(newPosition.adGroupIndex);
currentAdIndexInAdGroupRef.set(newPosition.adIndexInAdGroup);
latch.countDown();
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
shuffleModeEnabledRef.set(shuffleModeEnabled);
latch.countDown();
}
@Override
public void onRepeatModeChanged(@RepeatMode int repeatMode) {
repeatModeRef.set(repeatMode);
latch.countDown();
}
}));
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(testState).setAudioAttributes(testAudioAttributes).setTimeline(testTimeline).setPlaylistMetadata(testPlaylistMetadata).setShuffleModeEnabled(testShuffleModeEnabled).setRepeatMode(testRepeatMode).setCurrentAdGroupIndex(testCurrentAdGroupIndex).setCurrentAdIndexInAdGroup(testCurrentAdIndexInAdGroup).build();
remoteSession.setPlayer(playerConfig);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(stateRef.get()).isEqualTo(testState);
MediaTestUtils.assertMediaIdEquals(testTimeline, timelineRef.get());
assertThat(playlistMetadataRef.get()).isEqualTo(testPlaylistMetadata);
assertThat(audioAttributesRef.get()).isEqualTo(testAudioAttributes);
assertThat(currentAdGroupIndexRef.get()).isEqualTo(testCurrentAdGroupIndex);
assertThat(currentAdIndexInAdGroupRef.get()).isEqualTo(testCurrentAdIndexInAdGroup);
assertThat(shuffleModeEnabledRef.get()).isEqualTo(testShuffleModeEnabled);
assertThat(repeatModeRef.get()).isEqualTo(testRepeatMode);
}
use of androidx.media3.common.AudioAttributes in project media by androidx.
the class MediaControllerListenerTest method onAudioAttributesChanged_isCalledAndUpdatesGetter.
@Test
public void onAudioAttributesChanged_isCalledAndUpdatesGetter() throws Exception {
AudioAttributes testAttributes = new AudioAttributes.Builder().setUsage(C.USAGE_MEDIA).setContentType(C.CONTENT_TYPE_MOVIE).build();
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<AudioAttributes> attributesRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onAudioAttributesChanged(AudioAttributes attributes) {
if (testAttributes.equals(attributes)) {
attributesRef.set(controller.getAudioAttributes());
latch.countDown();
}
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
remoteSession.getMockPlayer().notifyAudioAttributesChanged(testAttributes);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(attributesRef.get()).isEqualTo(testAttributes);
}
use of androidx.media3.common.AudioAttributes in project media by androidx.
the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_playbackTypeChangedToLocal.
@Test
public void setPlayer_playbackTypeChangedToLocal() throws Exception {
DeviceInfo deviceInfo = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_REMOTE, /* minVolume= */
0, /* maxVolume= */
10);
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfo).build();
session.setPlayer(playerConfig);
DeviceInfo deviceInfoToUpdate = new DeviceInfo(DeviceInfo.PLAYBACK_TYPE_LOCAL, /* minVolume= */
0, /* maxVolume= */
10);
int legacyPlaybackTypeToUpdate = 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() == legacyPlaybackTypeToUpdate && info.getAudioAttributes().getLegacyStreamType() == legacyStream) {
playbackInfoNotified.countDown();
}
}
};
controllerCompat.registerCallback(callback, handler);
Bundle playerConfigToUpdate = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceInfo(deviceInfoToUpdate).setAudioAttributes(attrs).build();
session.setPlayer(playerConfigToUpdate);
// In API 21 and 22, onAudioInfoChanged is not called when playback is changed to local.
if (Util.SDK_INT == 21 || Util.SDK_INT == 22) {
PollingCheck.waitFor(TIMEOUT_MS, () -> {
MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
return info.getPlaybackType() == legacyPlaybackTypeToUpdate && info.getAudioAttributes().getLegacyStreamType() == legacyStream;
});
} else {
assertThat(playbackInfoNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
MediaControllerCompat.PlaybackInfo info = controllerCompat.getPlaybackInfo();
assertThat(info.getPlaybackType()).isEqualTo(legacyPlaybackTypeToUpdate);
assertThat(info.getAudioAttributes().getLegacyStreamType()).isEqualTo(legacyStream);
}
}
Aggregations