Search in sources :

Example 26 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class ImaServerSideAdInsertionMediaSource method setContentUri.

// Internal methods (called on the playback thread).
private void setContentUri(Uri contentUri) {
    if (serverSideAdInsertionMediaSource != null) {
        return;
    }
    MediaItem contentMediaItem = new MediaItem.Builder().setUri(contentUri).setDrmConfiguration(checkNotNull(mediaItem.localConfiguration).drmConfiguration).setLiveConfiguration(mediaItem.liveConfiguration).setCustomCacheKey(mediaItem.localConfiguration.customCacheKey).setStreamKeys(mediaItem.localConfiguration.streamKeys).build();
    ServerSideAdInsertionMediaSource serverSideAdInsertionMediaSource = new ServerSideAdInsertionMediaSource(contentMediaSourceFactory.createMediaSource(contentMediaItem), componentListener);
    this.serverSideAdInsertionMediaSource = serverSideAdInsertionMediaSource;
    if (isLiveStream) {
        AdPlaybackState liveAdPlaybackState = new AdPlaybackState(adsId).withNewAdGroup(/* adGroupIndex= */
        0, /* adGroupTimeUs= */
        C.TIME_END_OF_SOURCE).withIsServerSideInserted(/* adGroupIndex= */
        0, true);
        mainHandler.post(() -> setAdPlaybackState(liveAdPlaybackState));
    }
    prepareChildSource(/* id= */
    null, serverSideAdInsertionMediaSource);
}
Also used : ServerSideAdInsertionMediaSource(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource) ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState(com.google.android.exoplayer2.source.ads.ServerSideAdInsertionUtil.addAdGroupToAdPlaybackState) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) MediaItem(com.google.android.exoplayer2.MediaItem)

Example 27 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class SessionCallbackBuilderTest method allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable.

@Test
public void allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable() throws Exception {
    List<MediaItem> testPlaylist = new ArrayList<>();
    testPlaylist.add(TestUtils.createMediaItem(R.raw.video_desks));
    UriMediaItem secondPlaylistItem = TestUtils.createMediaItem(R.raw.video_big_buck_bunny);
    testPlaylist.add(secondPlaylistItem);
    CountDownLatch readAllowedLatch = new CountDownLatch(1);
    playerTestRule.setDataSourceInstrumentation(dataSpec -> {
        if (dataSpec.uri.equals(secondPlaylistItem.getUri())) {
            try {
                assertThat(readAllowedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
            } catch (Exception e) {
                assertWithMessage("Unexpected exception %s", e).fail();
            }
        }
    });
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
        assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(testPlaylist, null));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        CountDownLatch seekToAllowedForSecondMediaItem = new CountDownLatch(1);
        OnAllowedCommandsChangedListener allowedCommandsChangedListener = (controller, allowedCommands) -> {
            if (allowedCommands.hasCommand(SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO) && controller.getCurrentMediaItemIndex() == 1) {
                seekToAllowedForSecondMediaItem.countDown();
            }
        };
        try (MediaController controller = createConnectedController(session, /* onConnectedListener= */
        null, allowedCommandsChangedListener)) {
            assertPlayerResultSuccess(sessionPlayerConnector.skipToNextPlaylistItem());
            readAllowedLatch.countDown();
            assertThat(seekToAllowedForSecondMediaItem.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) UriMediaItem(androidx.media2.common.UriMediaItem) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 28 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class PlayerWrapper method addPlaylistItem.

public boolean addPlaylistItem(int index, androidx.media2.common.MediaItem media2MediaItem) {
    Assertions.checkArgument(!media2Playlist.contains(media2MediaItem));
    index = Util.constrainValue(index, 0, media2Playlist.size());
    MediaItem exoPlayerMediaItem = Assertions.checkNotNull(mediaItemConverter.convertToExoPlayerMediaItem(media2MediaItem));
    player.addMediaItem(index, exoPlayerMediaItem);
    return true;
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem)

Example 29 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class PlayerWrapper method setPlaylist.

public boolean setPlaylist(List<androidx.media2.common.MediaItem> playlist, @Nullable MediaMetadata metadata) {
    // Check for duplication.
    for (int i = 0; i < playlist.size(); i++) {
        androidx.media2.common.MediaItem media2MediaItem = playlist.get(i);
        Assertions.checkArgument(playlist.indexOf(media2MediaItem) == i);
    }
    this.playlistMetadata = metadata;
    List<MediaItem> exoPlayerMediaItems = new ArrayList<>();
    for (int i = 0; i < playlist.size(); i++) {
        androidx.media2.common.MediaItem media2MediaItem = playlist.get(i);
        MediaItem exoPlayerMediaItem = Assertions.checkNotNull(mediaItemConverter.convertToExoPlayerMediaItem(media2MediaItem));
        exoPlayerMediaItems.add(exoPlayerMediaItem);
    }
    player.setMediaItems(exoPlayerMediaItems, /* resetPosition= */
    true);
    currentWindowIndex = getCurrentMediaItemIndex();
    return true;
}
Also used : CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList)

Example 30 with MediaItem

use of com.google.android.exoplayer2.MediaItem in project ExoPlayer by google.

the class PlayerWrapper method updatePlaylist.

private void updatePlaylist(Timeline timeline) {
    List<androidx.media2.common.MediaItem> media2MediaItemToBeRemoved = new ArrayList<>(media2Playlist);
    media2Playlist.clear();
    exoPlayerPlaylist.clear();
    Timeline.Window window = new Timeline.Window();
    int windowCount = timeline.getWindowCount();
    for (int i = 0; i < windowCount; i++) {
        timeline.getWindow(i, window);
        MediaItem exoPlayerMediaItem = window.mediaItem;
        androidx.media2.common.MediaItem media2MediaItem = Assertions.checkNotNull(mediaItemConverter.convertToMedia2MediaItem(exoPlayerMediaItem));
        exoPlayerPlaylist.add(exoPlayerMediaItem);
        media2Playlist.add(media2MediaItem);
        media2MediaItemToBeRemoved.remove(media2MediaItem);
    }
    for (androidx.media2.common.MediaItem item : media2MediaItemToBeRemoved) {
        releaseMediaItem(item);
    }
}
Also used : Timeline(com.google.android.exoplayer2.Timeline) CallbackMediaItem(androidx.media2.common.CallbackMediaItem) MediaItem(com.google.android.exoplayer2.MediaItem) ArrayList(java.util.ArrayList)

Aggregations

MediaItem (com.google.android.exoplayer2.MediaItem)160 Test (org.junit.Test)151 MediaSource (com.google.android.exoplayer2.source.MediaSource)31 ArrayList (java.util.ArrayList)29 Timeline (com.google.android.exoplayer2.Timeline)25 InOrder (org.mockito.InOrder)20 Nullable (androidx.annotation.Nullable)19 AdsMediaSource (com.google.android.exoplayer2.source.ads.AdsMediaSource)19 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)19 Player (com.google.android.exoplayer2.Player)17 Uri (android.net.Uri)15 Listener (com.google.android.exoplayer2.Player.Listener)15 TestPlayerRunHelper.playUntilStartOfMediaItem (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem)15 DefaultMediaSourceFactory (com.google.android.exoplayer2.source.DefaultMediaSourceFactory)15 AnalyticsListener (com.google.android.exoplayer2.analytics.AnalyticsListener)13 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)13 TransferListener (com.google.android.exoplayer2.upstream.TransferListener)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 DrmSessionEventListener (com.google.android.exoplayer2.drm.DrmSessionEventListener)12 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)12