Search in sources :

Example 6 with AdsManager

use of com.google.ads.interactivemedia.v3.api.AdsManager in project ExoPlayer by google.

the class AdTagLoader method maybeInitializeAdsManager.

private void maybeInitializeAdsManager(long contentPositionMs, long contentDurationMs) {
    @Nullable AdsManager adsManager = this.adsManager;
    if (!isAdsManagerInitialized && adsManager != null) {
        isAdsManagerInitialized = true;
        @Nullable AdsRenderingSettings adsRenderingSettings = setupAdsRendering(contentPositionMs, contentDurationMs);
        if (adsRenderingSettings == null) {
            // There are no ads to play.
            destroyAdsManager();
        } else {
            adsManager.init(adsRenderingSettings);
            adsManager.start();
            if (configuration.debugModeEnabled) {
                Log.d(TAG, "Initialized with ads rendering settings: " + adsRenderingSettings);
            }
        }
        updateAdPlaybackState();
    }
}
Also used : AdsRenderingSettings(com.google.ads.interactivemedia.v3.api.AdsRenderingSettings) AdsManager(com.google.ads.interactivemedia.v3.api.AdsManager) Nullable(androidx.annotation.Nullable)

Example 7 with AdsManager

use of com.google.ads.interactivemedia.v3.api.AdsManager in project ExoPlayer by google.

the class AdTagLoader method deactivate.

/**
 * Deactivates playback.
 */
public void deactivate() {
    Player player = checkNotNull(this.player);
    if (!AdPlaybackState.NONE.equals(adPlaybackState) && imaPausedContent) {
        if (adsManager != null) {
            adsManager.pause();
        }
        adPlaybackState = adPlaybackState.withAdResumePositionUs(playingAd ? Util.msToUs(player.getCurrentPosition()) : 0);
    }
    lastVolumePercent = getPlayerVolumePercent();
    lastAdProgress = getAdVideoProgressUpdate();
    lastContentProgress = getContentVideoProgressUpdate();
    player.removeListener(this);
    this.player = null;
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer)

Example 8 with AdsManager

use of com.google.ads.interactivemedia.v3.api.AdsManager in project ExoPlayer by google.

the class AdTagLoader method handleTimelineOrPositionChanged.

private void handleTimelineOrPositionChanged() {
    @Nullable Player player = this.player;
    if (adsManager == null || player == null) {
        return;
    }
    if (!playingAd && !player.isPlayingAd()) {
        ensureSentContentCompleteIfAtEndOfStream();
        if (!sentContentComplete && !timeline.isEmpty()) {
            long positionMs = getContentPeriodPositionMs(player, timeline, period);
            timeline.getPeriod(player.getCurrentPeriodIndex(), period);
            int newAdGroupIndex = period.getAdGroupIndexForPositionUs(Util.msToUs(positionMs));
            if (newAdGroupIndex != C.INDEX_UNSET) {
                sentPendingContentPositionMs = false;
                pendingContentPositionMs = positionMs;
            }
        }
    }
    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.
        @Nullable AdMediaInfo adMediaInfo = imaAdMediaInfo;
        if (adMediaInfo == null) {
            Log.w(TAG, "onEnded without ad media info");
        } else {
            @Nullable AdInfo adInfo = adInfoByAdMediaInfo.get(adMediaInfo);
            if (playingAdIndexInAdGroup == C.INDEX_UNSET || (adInfo != null && adInfo.adIndexInAdGroup < playingAdIndexInAdGroup)) {
                for (int i = 0; i < adCallbacks.size(); i++) {
                    adCallbacks.get(i).onEnded(adMediaInfo);
                }
                if (configuration.debugModeEnabled) {
                    Log.d(TAG, "VideoAdPlayerCallback.onEnded in onTimelineChanged/onPositionDiscontinuity");
                }
            }
        }
    }
    if (!sentContentComplete && !wasPlayingAd && playingAd && imaAdState == IMA_AD_STATE_NONE) {
        AdPlaybackState.AdGroup adGroup = adPlaybackState.getAdGroup(player.getCurrentAdGroupIndex());
        if (adGroup.timeUs == C.TIME_END_OF_SOURCE) {
            sendContentComplete();
        } else {
            // IMA hasn't called playAd yet, so fake the content position.
            fakeContentProgressElapsedRealtimeMs = SystemClock.elapsedRealtime();
            fakeContentProgressOffsetMs = Util.usToMs(adGroup.timeUs);
            if (fakeContentProgressOffsetMs == C.TIME_END_OF_SOURCE) {
                fakeContentProgressOffsetMs = contentDurationMs;
            }
        }
    }
}
Also used : Player(com.google.android.exoplayer2.Player) VideoAdPlayer(com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer) AdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState) AdMediaInfo(com.google.ads.interactivemedia.v3.api.player.AdMediaInfo) Nullable(androidx.annotation.Nullable)

Example 9 with AdsManager

use of com.google.ads.interactivemedia.v3.api.AdsManager in project android-player-samples by BrightcoveOS.

the class MainActivity method setupAdMarkers.

/*
      This methods show how to the the Google IMA AdsManager, get the cue points and add the markers
      to the Brightcove Seek Bar.
     */
private void setupAdMarkers(BaseVideoView videoView) {
    final BrightcoveMediaController mediaController = new BrightcoveMediaController(brightcoveVideoView);
    // Add "Ad Markers" where the Ads Manager says ads will appear.
    mediaController.addListener(GoogleIMAEventType.ADS_MANAGER_LOADED, event -> {
        AdsManager manager = (AdsManager) event.properties.get("adsManager");
        if (manager != null) {
            List<Float> cuepoints = manager.getAdCuePoints();
            for (int i = 0; i < cuepoints.size(); i++) {
                Float cuepoint = cuepoints.get(i);
                BrightcoveSeekBar brightcoveSeekBar = mediaController.getBrightcoveSeekBar();
                // If cuepoint is negative it means it is a POST ROLL.
                int markerTime = cuepoint < 0 ? brightcoveSeekBar.getMax() : (int) (cuepoint * DateUtils.SECOND_IN_MILLIS);
                mediaController.getBrightcoveSeekBar().addMarker(markerTime);
            }
        }
    });
    videoView.setMediaController(mediaController);
}
Also used : AdsManager(com.google.ads.interactivemedia.v3.api.AdsManager) BrightcoveSeekBar(com.brightcove.player.mediacontroller.BrightcoveSeekBar) BrightcoveMediaController(com.brightcove.player.mediacontroller.BrightcoveMediaController)

Aggregations

Nullable (androidx.annotation.Nullable)4 AdsManager (com.google.ads.interactivemedia.v3.api.AdsManager)4 VideoAdPlayer (com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer)3 Player (com.google.android.exoplayer2.Player)3 BrightcoveMediaController (com.brightcove.player.mediacontroller.BrightcoveMediaController)2 AdMediaInfo (com.google.ads.interactivemedia.v3.api.player.AdMediaInfo)2 SurfaceView (android.view.SurfaceView)1 View (android.view.View)1 ImageButton (android.widget.ImageButton)1 ImageView (android.widget.ImageView)1 Event (com.brightcove.player.event.Event)1 EventListener (com.brightcove.player.event.EventListener)1 Catalog (com.brightcove.player.media.Catalog)1 VideoListener (com.brightcove.player.media.VideoListener)1 BrightcoveSeekBar (com.brightcove.player.mediacontroller.BrightcoveSeekBar)1 Video (com.brightcove.player.model.Video)1 AdsLoader (com.google.ads.interactivemedia.v3.api.AdsLoader)1 AdsManagerLoadedEvent (com.google.ads.interactivemedia.v3.api.AdsManagerLoadedEvent)1 AdsRenderingSettings (com.google.ads.interactivemedia.v3.api.AdsRenderingSettings)1 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)1