private boolean isPlayingAd() { return player != null && player.isPlayingAd() && player.getPlayWhenReady(); }
boolean isPlayingAd = player.isPlayingAd(); List<String> stringActions = new ArrayList<>(); if (!isPlayingAd) {
private void updateImaStateForPlayerState() { boolean wasPlayingAd = playingAd; int oldPlayingAdIndexInAdGroup = playingAdIndexInAdGroup; playingAd = player.isPlayingAd(); playingAdIndexInAdGroup = playingAd ? player.getCurrentAdIndexInAdGroup() : C.INDEX_UNSET; boolean adFinished = wasPlayingAd && playingAdIndexInAdGroup != oldPlayingAdIndexInAdGroup; if (adFinished) { // IMA is waiting for the ad playback to finish so invoke the callback now. // Either CONTENT_RESUME_REQUESTED will be passed next, or playAd will be called again. for (int i = 0; i < adCallbacks.size(); i++) { adCallbacks.get(i).onEnded(); } if (DEBUG) { Log.d(TAG, "VideoAdPlayerCallback.onEnded in onTimelineChanged/onPositionDiscontinuity"); } } if (!sentContentComplete && !wasPlayingAd && playingAd && imaAdState == IMA_AD_STATE_NONE) { int adGroupIndex = player.getCurrentAdGroupIndex(); // IMA hasn't called playAd yet, so fake the content position. fakeContentProgressElapsedRealtimeMs = SystemClock.elapsedRealtime(); fakeContentProgressOffsetMs = C.usToMs(adPlaybackState.adGroupTimesUs[adGroupIndex]); if (fakeContentProgressOffsetMs == C.TIME_END_OF_SOURCE) { fakeContentProgressOffsetMs = contentDurationMs; } } }
@Override public void onSkipToQueueItem(Player player, long id) { Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return; } int windowIndex = (int) id; if (0 <= windowIndex && windowIndex < timeline.getWindowCount()) { player.seekTo(windowIndex, C.TIME_UNSET); } }
@Override public boolean dispatchKeyEvent(KeyEvent event) { if (player != null && player.isPlayingAd()) { // Focus any overlay UI now, in case it's provided by a WebView whose contents may update // dynamically. This is needed to make the "Skip ad" button focused on Android TV when using // IMA [Internal: b/62371030]. overlayFrameLayout.requestFocus(); return super.dispatchKeyEvent(event); } boolean isDpadWhenControlHidden = isDpadKey(event.getKeyCode()) && useController && !controller.isVisible(); boolean handled = isDpadWhenControlHidden || dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event); if (handled) { maybeShowController(true); } return handled; }
@Override public long getSupportedQueueNavigatorActions(Player player) { if (player == null) { return 0; } Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return 0; } long actions = 0; if (timeline.getWindowCount() > 1) { actions |= PlaybackStateCompat.ACTION_SKIP_TO_QUEUE_ITEM; } if (window.isSeekable || !window.isDynamic || player.hasPrevious()) { actions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS; } if (window.isDynamic || player.hasNext()) { actions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT; } return actions; }
private void next() { Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return; } int windowIndex = player.getCurrentWindowIndex(); int nextWindowIndex = player.getNextWindowIndex(); if (nextWindowIndex != C.INDEX_UNSET) { seekTo(nextWindowIndex, C.TIME_UNSET); } else if (timeline.getWindow(windowIndex, window).isDynamic) { seekTo(windowIndex, C.TIME_UNSET); } }
@Override public void onSkipToNext(Player player) { Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return; } int windowIndex = player.getCurrentWindowIndex(); int nextWindowIndex = player.getNextWindowIndex(); if (nextWindowIndex != C.INDEX_UNSET) { player.seekTo(nextWindowIndex, C.TIME_UNSET); } else if (timeline.getWindow(windowIndex, window).isDynamic) { player.seekTo(windowIndex, C.TIME_UNSET); } }
private void previous() { Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return; } int windowIndex = player.getCurrentWindowIndex(); timeline.getWindow(windowIndex, window); int previousWindowIndex = player.getPreviousWindowIndex(); if (previousWindowIndex != C.INDEX_UNSET && (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS || (window.isDynamic && !window.isSeekable))) { seekTo(previousWindowIndex, C.TIME_UNSET); } else { seekTo(0); } }
@Override public void onSkipToPrevious(Player player) { Timeline timeline = player.getCurrentTimeline(); if (timeline.isEmpty() || player.isPlayingAd()) { return; } int windowIndex = player.getCurrentWindowIndex(); timeline.getWindow(windowIndex, window); int previousWindowIndex = player.getPreviousWindowIndex(); if (previousWindowIndex != C.INDEX_UNSET && (player.getCurrentPosition() <= MAX_POSITION_FOR_SEEK_TO_PREVIOUS || (window.isDynamic && !window.isSeekable))) { player.seekTo(previousWindowIndex, C.TIME_UNSET); } else { player.seekTo(0); } }
.setDefaults(defaults); if (useChronometer && !player.isPlayingAd() && !player.isCurrentWindowDynamic() && player.getPlayWhenReady()
@Override public void onPositionDiscontinuity(@Player.DiscontinuityReason int reason) { if (adsManager == null) { return; } if (!playingAd && !player.isPlayingAd()) { checkForContentComplete(); if (sentContentComplete) { for (int i = 0; i < adPlaybackState.adGroupCount; i++) { if (adPlaybackState.adGroupTimesUs[i] != C.TIME_END_OF_SOURCE) { adPlaybackState = adPlaybackState.withSkippedAdGroup(i); } } updateAdPlaybackState(); } else { long positionMs = player.getCurrentPosition(); timeline.getPeriod(0, period); int newAdGroupIndex = period.getAdGroupIndexForPositionUs(C.msToUs(positionMs)); if (newAdGroupIndex != C.INDEX_UNSET) { sentPendingContentPositionMs = false; pendingContentPositionMs = positionMs; if (newAdGroupIndex != adGroupIndex) { shouldNotifyAdPrepareError = false; } } } } else { updateImaStateForPlayerState(); } }
private void updateNavigation() { if (!isVisible() || !isAttachedToWindow) { return; } Timeline timeline = player != null ? player.getCurrentTimeline() : null; boolean haveNonEmptyTimeline = timeline != null && !timeline.isEmpty(); boolean isSeekable = false; boolean enablePrevious = false; boolean enableNext = false; if (haveNonEmptyTimeline && !player.isPlayingAd()) { int windowIndex = player.getCurrentWindowIndex(); timeline.getWindow(windowIndex, window); isSeekable = window.isSeekable; enablePrevious = isSeekable || !window.isDynamic || player.hasPrevious(); enableNext = window.isDynamic || player.hasNext(); } setButtonEnabled(enablePrevious, previousButton); setButtonEnabled(enableNext, nextButton); setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton); setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton); if (timeBar != null) { timeBar.setEnabled(isSeekable); } }
if (player.isPlayingAd()) { builder.putLong(MediaMetadataCompat.METADATA_KEY_ADVERTISEMENT, 1);
private boolean isPlayingAd() { return player != null && player.isPlayingAd() && player.getPlayWhenReady(); }
@Override public boolean dispatchKeyEvent(KeyEvent event) { if (player != null && player.isPlayingAd()) { // Focus any overlay UI now, in case it's provided by a WebView whose contents may update // dynamically. This is needed to make the "Skip ad" button focused on Android TV when using // IMA [Internal: b/62371030]. overlayFrameLayout.requestFocus(); return super.dispatchKeyEvent(event); } boolean isDpadWhenControlHidden = isDpadKey(event.getKeyCode()) && useController && !controller.isVisible(); maybeShowController(true); return isDpadWhenControlHidden || dispatchMediaKeyEvent(event) || super.dispatchKeyEvent(event); }
position = C.usToMs(currentWindowTimeBarOffsetUs); bufferedPosition = position; if (player.isPlayingAd()) { position += player.getContentPosition(); bufferedPosition = position;
private void updateNavigation() { if (!isVisible() || !isAttachedToWindow) { return; } Timeline timeline = player != null ? player.getCurrentTimeline() : null; boolean haveNonEmptyTimeline = timeline != null && !timeline.isEmpty(); boolean isSeekable = false; boolean enablePrevious = false; boolean enableNext = false; if (haveNonEmptyTimeline && !player.isPlayingAd()) { int windowIndex = player.getCurrentWindowIndex(); timeline.getWindow(windowIndex, window); isSeekable = window.isSeekable; enablePrevious = isSeekable || !window.isDynamic || player.getPreviousWindowIndex() != C.INDEX_UNSET; enableNext = window.isDynamic || player.getNextWindowIndex() != C.INDEX_UNSET; } setButtonEnabled(enablePrevious, previousButton); setButtonEnabled(enableNext, nextButton); setButtonEnabled(fastForwardMs > 0 && isSeekable, fastForwardButton); setButtonEnabled(rewindMs > 0 && isSeekable, rewindButton); if (timeBar != null) { timeBar.setEnabled(isSeekable && !isHls); } if (timeBarLandscape != null) { timeBarLandscape.setEnabled(isSeekable && !isHls); } }