Search in sources :

Example 1 with AdsRenderingSettings

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

the class AdTagLoader method setupAdsRendering.

/**
 * Configures ads rendering for starting playback, returning the settings for the IMA SDK or
 * {@code null} if no ads should play.
 */
@Nullable
private AdsRenderingSettings setupAdsRendering(long contentPositionMs, long contentDurationMs) {
    AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
    adsRenderingSettings.setEnablePreloading(true);
    adsRenderingSettings.setMimeTypes(configuration.adMediaMimeTypes != null ? configuration.adMediaMimeTypes : supportedMimeTypes);
    if (configuration.mediaLoadTimeoutMs != TIMEOUT_UNSET) {
        adsRenderingSettings.setLoadVideoTimeout(configuration.mediaLoadTimeoutMs);
    }
    if (configuration.mediaBitrate != BITRATE_UNSET) {
        adsRenderingSettings.setBitrateKbps(configuration.mediaBitrate / 1000);
    }
    adsRenderingSettings.setFocusSkipButtonWhenAvailable(configuration.focusSkipButtonWhenAvailable);
    if (configuration.adUiElements != null) {
        adsRenderingSettings.setUiElements(configuration.adUiElements);
    }
    // Skip ads based on the start position as required.
    int adGroupForPositionIndex = adPlaybackState.getAdGroupIndexForPositionUs(Util.msToUs(contentPositionMs), Util.msToUs(contentDurationMs));
    if (adGroupForPositionIndex != C.INDEX_UNSET) {
        boolean playAdWhenStartingPlayback = adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs == Util.msToUs(contentPositionMs) || configuration.playAdBeforeStartPosition;
        if (!playAdWhenStartingPlayback) {
            adGroupForPositionIndex++;
        } else if (hasMidrollAdGroups(adPlaybackState)) {
            // Provide the player's initial position to trigger loading and playing the ad. If there are
            // no midrolls, we are playing a preroll and any pending content position wouldn't be
            // cleared.
            pendingContentPositionMs = contentPositionMs;
        }
        if (adGroupForPositionIndex > 0) {
            for (int i = 0; i < adGroupForPositionIndex; i++) {
                adPlaybackState = adPlaybackState.withSkippedAdGroup(i);
            }
            if (adGroupForPositionIndex == adPlaybackState.adGroupCount) {
                // ads, we signal that no ads will render so the caller can destroy the ads manager.
                return null;
            }
            long adGroupForPositionTimeUs = adPlaybackState.getAdGroup(adGroupForPositionIndex).timeUs;
            long adGroupBeforePositionTimeUs = adPlaybackState.getAdGroup(adGroupForPositionIndex - 1).timeUs;
            if (adGroupForPositionTimeUs == C.TIME_END_OF_SOURCE) {
                // Play the postroll by offsetting the start position just past the last non-postroll ad.
                adsRenderingSettings.setPlayAdsAfterTime((double) adGroupBeforePositionTimeUs / C.MICROS_PER_SECOND + 1d);
            } else {
                // Play ads after the midpoint between the ad to play and the one before it, to avoid
                // issues with rounding one of the two ad times.
                double midpointTimeUs = (adGroupForPositionTimeUs + adGroupBeforePositionTimeUs) / 2d;
                adsRenderingSettings.setPlayAdsAfterTime(midpointTimeUs / C.MICROS_PER_SECOND);
            }
        }
    }
    return adsRenderingSettings;
}
Also used : AdsRenderingSettings(com.google.ads.interactivemedia.v3.api.AdsRenderingSettings) Nullable(androidx.annotation.Nullable)

Example 2 with AdsRenderingSettings

use of com.google.ads.interactivemedia.v3.api.AdsRenderingSettings 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)

Aggregations

Nullable (androidx.annotation.Nullable)2 AdsRenderingSettings (com.google.ads.interactivemedia.v3.api.AdsRenderingSettings)2 AdsManager (com.google.ads.interactivemedia.v3.api.AdsManager)1