Search in sources :

Example 46 with Player

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

the class ImaAdsLoaderTest method playback_withMidrollFetchError_updatesContentProgress.

@Test
public void playback_withMidrollFetchError_updatesContentProgress() {
    AdEvent mockMidrollFetchErrorAdEvent = mock(AdEvent.class);
    when(mockMidrollFetchErrorAdEvent.getType()).thenReturn(AdEventType.AD_BREAK_FETCH_ERROR);
    when(mockMidrollFetchErrorAdEvent.getAdData()).thenReturn(ImmutableMap.of("adBreakTime", "5.5"));
    when(mockAdsManager.getAdCuePoints()).thenReturn(ImmutableList.of(5.5f));
    // Simulate loading an empty midroll ad and advancing the player position.
    imaAdsLoader.start(adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
    adEventListener.onAdEvent(mockMidrollFetchErrorAdEvent);
    long playerPositionUs = CONTENT_DURATION_US - C.MICROS_PER_SECOND;
    long playerPositionInPeriodUs = playerPositionUs + TimelineWindowDefinition.DEFAULT_WINDOW_OFFSET_IN_FIRST_PERIOD_US;
    long periodDurationUs = CONTENT_TIMELINE.getPeriod(/* periodIndex= */
    0, new Period()).durationUs;
    fakePlayer.setPlayingContentPosition(/* periodIndex= */
    0, Util.usToMs(playerPositionUs));
    // Verify the content progress is updated to reflect the new player position.
    assertThat(contentProgressProvider.getContentProgress()).isEqualTo(new VideoProgressUpdate(Util.usToMs(playerPositionInPeriodUs), Util.usToMs(periodDurationUs)));
}
Also used : Period(com.google.android.exoplayer2.Timeline.Period) AdEvent(com.google.ads.interactivemedia.v3.api.AdEvent) VideoProgressUpdate(com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate) Test(org.junit.Test)

Example 47 with Player

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

the class MediaSessionConnector method invalidateMediaSessionPlaybackState.

/**
 * Updates the playback state of the media session.
 *
 * <p>Apps normally only need to call this method when the custom actions provided by a {@link
 * CustomActionProvider} changed and the playback state needs to be updated immediately.
 */
public final void invalidateMediaSessionPlaybackState() {
    PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder();
    @Nullable Player player = this.player;
    if (player == null) {
        builder.setActions(buildPrepareActions()).setState(PlaybackStateCompat.STATE_NONE, /* position= */
        0, /* playbackSpeed= */
        0, /* updateTime= */
        SystemClock.elapsedRealtime());
        mediaSession.setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
        mediaSession.setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
        mediaSession.setPlaybackState(builder.build());
        return;
    }
    Map<String, CustomActionProvider> currentActions = new HashMap<>();
    for (CustomActionProvider customActionProvider : customActionProviders) {
        @Nullable PlaybackStateCompat.CustomAction customAction = customActionProvider.getCustomAction(player);
        if (customAction != null) {
            currentActions.put(customAction.getAction(), customActionProvider);
            builder.addCustomAction(customAction);
        }
    }
    customActionMap = Collections.unmodifiableMap(currentActions);
    Bundle extras = new Bundle();
    @Nullable PlaybackException playbackError = player.getPlayerError();
    boolean reportError = playbackError != null || customError != null;
    int sessionPlaybackState = reportError ? PlaybackStateCompat.STATE_ERROR : getMediaSessionPlaybackState(player.getPlaybackState(), player.getPlayWhenReady());
    if (customError != null) {
        builder.setErrorMessage(customError.first, customError.second);
        if (customErrorExtras != null) {
            extras.putAll(customErrorExtras);
        }
    } else if (playbackError != null && errorMessageProvider != null) {
        Pair<Integer, String> message = errorMessageProvider.getErrorMessage(playbackError);
        builder.setErrorMessage(message.first, message.second);
    }
    long activeQueueItemId = queueNavigator != null ? queueNavigator.getActiveQueueItemId(player) : MediaSessionCompat.QueueItem.UNKNOWN_ID;
    float playbackSpeed = player.getPlaybackParameters().speed;
    extras.putFloat(EXTRAS_SPEED, playbackSpeed);
    float sessionPlaybackSpeed = player.isPlaying() ? playbackSpeed : 0f;
    @Nullable MediaItem currentMediaItem = player.getCurrentMediaItem();
    if (currentMediaItem != null && !MediaItem.DEFAULT_MEDIA_ID.equals(currentMediaItem.mediaId)) {
        extras.putString(PLAYBACK_STATE_EXTRAS_KEY_MEDIA_ID, currentMediaItem.mediaId);
    }
    builder.setActions(buildPrepareActions() | buildPlaybackActions(player)).setActiveQueueItemId(activeQueueItemId).setBufferedPosition(player.getBufferedPosition()).setState(sessionPlaybackState, player.getCurrentPosition(), sessionPlaybackSpeed, /* updateTime= */
    SystemClock.elapsedRealtime()).setExtras(extras);
    @Player.RepeatMode int repeatMode = player.getRepeatMode();
    mediaSession.setRepeatMode(repeatMode == Player.REPEAT_MODE_ONE ? PlaybackStateCompat.REPEAT_MODE_ONE : repeatMode == Player.REPEAT_MODE_ALL ? PlaybackStateCompat.REPEAT_MODE_ALL : PlaybackStateCompat.REPEAT_MODE_NONE);
    mediaSession.setShuffleMode(player.getShuffleModeEnabled() ? PlaybackStateCompat.SHUFFLE_MODE_ALL : PlaybackStateCompat.SHUFFLE_MODE_NONE);
    mediaSession.setPlaybackState(builder.build());
}
Also used : PlaybackException(com.google.android.exoplayer2.PlaybackException) Player(com.google.android.exoplayer2.Player) HashMap(java.util.HashMap) Bundle(android.os.Bundle) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaItem(com.google.android.exoplayer2.MediaItem) Nullable(androidx.annotation.Nullable) Pair(android.util.Pair)

Example 48 with Player

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

the class ImaAdsLoader method start.

@Override
public void start(AdsMediaSource adsMediaSource, DataSpec adTagDataSpec, Object adsId, AdViewProvider adViewProvider, EventListener eventListener) {
    checkState(wasSetPlayerCalled, "Set player using adsLoader.setPlayer before preparing the player.");
    if (adTagLoaderByAdsMediaSource.isEmpty()) {
        player = nextPlayer;
        @Nullable Player player = this.player;
        if (player == null) {
            return;
        }
        player.addListener(playerListener);
    }
    @Nullable AdTagLoader adTagLoader = adTagLoaderByAdsId.get(adsId);
    if (adTagLoader == null) {
        requestAds(adTagDataSpec, adsId, adViewProvider.getAdViewGroup());
        adTagLoader = adTagLoaderByAdsId.get(adsId);
    }
    adTagLoaderByAdsMediaSource.put(adsMediaSource, checkNotNull(adTagLoader));
    adTagLoader.addListenerWithAdView(eventListener, adViewProvider);
    maybeUpdateCurrentAdTagLoader();
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer) Nullable(androidx.annotation.Nullable)

Example 49 with Player

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

the class ImaAdsLoader method maybePreloadNextPeriodAds.

private void maybePreloadNextPeriodAds() {
    @Nullable Player player = ImaAdsLoader.this.player;
    if (player == null) {
        return;
    }
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return;
    }
    int nextPeriodIndex = timeline.getNextPeriodIndex(player.getCurrentPeriodIndex(), period, window, player.getRepeatMode(), player.getShuffleModeEnabled());
    if (nextPeriodIndex == C.INDEX_UNSET) {
        return;
    }
    timeline.getPeriod(nextPeriodIndex, period);
    @Nullable Object nextAdsId = period.getAdsId();
    if (nextAdsId == null) {
        return;
    }
    @Nullable AdTagLoader nextAdTagLoader = adTagLoaderByAdsId.get(nextAdsId);
    if (nextAdTagLoader == null || nextAdTagLoader == currentAdTagLoader) {
        return;
    }
    long periodPositionUs = timeline.getPeriodPositionUs(window, period, period.windowIndex, /* windowPositionUs= */
    C.TIME_UNSET).second;
    nextAdTagLoader.maybePreloadAds(Util.usToMs(periodPositionUs), Util.usToMs(period.durationUs));
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer) Timeline(com.google.android.exoplayer2.Timeline) Nullable(androidx.annotation.Nullable)

Example 50 with Player

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

the class ServerSideAdInsertionUtil method getStreamPositionUs.

/**
 * Returns the position in the underlying server-side inserted ads stream for the current playback
 * position in the {@link Player}.
 *
 * @param player The {@link Player}.
 * @param adPlaybackState The {@link AdPlaybackState} defining the ad groups.
 * @return The position in the underlying server-side inserted ads stream, in microseconds, or
 *     {@link C#TIME_UNSET} if it can't be determined.
 */
public static long getStreamPositionUs(Player player, AdPlaybackState adPlaybackState) {
    Timeline timeline = player.getCurrentTimeline();
    if (timeline.isEmpty()) {
        return C.TIME_UNSET;
    }
    Timeline.Period period = timeline.getPeriod(player.getCurrentPeriodIndex(), new Timeline.Period());
    if (!Util.areEqual(period.getAdsId(), adPlaybackState.adsId)) {
        return C.TIME_UNSET;
    }
    if (player.isPlayingAd()) {
        int adGroupIndex = player.getCurrentAdGroupIndex();
        int adIndexInAdGroup = player.getCurrentAdIndexInAdGroup();
        long adPositionUs = Util.msToUs(player.getCurrentPosition());
        return getStreamPositionUsForAd(adPositionUs, adGroupIndex, adIndexInAdGroup, adPlaybackState);
    }
    long periodPositionUs = Util.msToUs(player.getCurrentPosition()) - period.getPositionInWindowUs();
    return getStreamPositionUsForContent(periodPositionUs, /* nextAdGroupIndex= */
    C.INDEX_UNSET, adPlaybackState);
}
Also used : Timeline(com.google.android.exoplayer2.Timeline)

Aggregations

Test (org.junit.Test)250 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)185 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)174 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)127 PlayerRunnable (com.google.android.exoplayer2.testutil.ActionSchedule.PlayerRunnable)107 ActionSchedule (com.google.android.exoplayer2.testutil.ActionSchedule)91 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)89 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)89 Listener (com.google.android.exoplayer2.Player.Listener)85 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)72 MediaSource (com.google.android.exoplayer2.source.MediaSource)68 ExoPlayerTestRunner (com.google.android.exoplayer2.testutil.ExoPlayerTestRunner)67 ConcatenatingMediaSource (com.google.android.exoplayer2.source.ConcatenatingMediaSource)66 Nullable (androidx.annotation.Nullable)56 ClippingMediaSource (com.google.android.exoplayer2.source.ClippingMediaSource)49 CompositeMediaSource (com.google.android.exoplayer2.source.CompositeMediaSource)49 MaskingMediaSource (com.google.android.exoplayer2.source.MaskingMediaSource)49 ServerSideAdInsertionMediaSource (com.google.android.exoplayer2.source.ads.ServerSideAdInsertionMediaSource)49 FakeAdaptiveMediaSource (com.google.android.exoplayer2.testutil.FakeAdaptiveMediaSource)49 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)47