Search in sources :

Example 1 with AdsLoader

use of androidx.media3.exoplayer.source.ads.AdsLoader in project media by androidx.

the class AdTagLoader method requestAds.

// Internal methods.
private AdsLoader requestAds(Context context, ImaSdkSettings imaSdkSettings, AdDisplayContainer adDisplayContainer) {
    AdsLoader adsLoader = imaFactory.createAdsLoader(context, imaSdkSettings, adDisplayContainer);
    adsLoader.addAdErrorListener(componentListener);
    if (configuration.applicationAdErrorListener != null) {
        adsLoader.addAdErrorListener(configuration.applicationAdErrorListener);
    }
    adsLoader.addAdsLoadedListener(componentListener);
    AdsRequest request;
    try {
        request = ImaUtil.getAdsRequestForAdTagDataSpec(imaFactory, adTagDataSpec);
    } catch (IOException e) {
        adPlaybackState = new AdPlaybackState(adsId);
        updateAdPlaybackState();
        pendingAdLoadError = AdLoadException.createForAllAds(e);
        maybeNotifyPendingAdLoadError();
        return adsLoader;
    }
    pendingAdRequestContext = new Object();
    request.setUserRequestContext(pendingAdRequestContext);
    if (configuration.enableContinuousPlayback != null) {
        request.setContinuousPlayback(configuration.enableContinuousPlayback);
    }
    if (configuration.vastLoadTimeoutMs != TIMEOUT_UNSET) {
        request.setVastLoadTimeout(configuration.vastLoadTimeoutMs);
    }
    request.setContentProgressProvider(componentListener);
    adsLoader.requestAds(request);
    return adsLoader;
}
Also used : AdsRequest(com.google.ads.interactivemedia.v3.api.AdsRequest) AdPlaybackState(androidx.media3.common.AdPlaybackState) IOException(java.io.IOException) AdsLoader(com.google.ads.interactivemedia.v3.api.AdsLoader)

Example 2 with AdsLoader

use of androidx.media3.exoplayer.source.ads.AdsLoader in project media by androidx.

the class AdsMediaSourceTest method setUp.

@Before
public void setUp() {
    // Set up content and ad media sources, passing a null timeline so tests can simulate setting it
    // later.
    contentMediaSource = new FakeMediaSource(/* timeline= */
    null);
    prerollAdMediaSource = new FakeMediaSource(/* timeline= */
    null);
    MediaSource.Factory adMediaSourceFactory = mock(MediaSource.Factory.class);
    when(adMediaSourceFactory.createMediaSource(any(MediaItem.class))).thenReturn(prerollAdMediaSource);
    // Prepare the AdsMediaSource and capture its ads loader listener.
    AdsLoader mockAdsLoader = mock(AdsLoader.class);
    AdViewProvider mockAdViewProvider = mock(AdViewProvider.class);
    ArgumentCaptor<EventListener> eventListenerArgumentCaptor = ArgumentCaptor.forClass(AdsLoader.EventListener.class);
    adsMediaSource = new AdsMediaSource(contentMediaSource, TEST_ADS_DATA_SPEC, TEST_ADS_ID, adMediaSourceFactory, mockAdsLoader, mockAdViewProvider);
    adsMediaSource.prepareSource(mockMediaSourceCaller, /* mediaTransferListener= */
    null, PlayerId.UNSET);
    shadowOf(Looper.getMainLooper()).idle();
    verify(mockAdsLoader).start(eq(adsMediaSource), eq(TEST_ADS_DATA_SPEC), eq(TEST_ADS_ID), eq(mockAdViewProvider), eventListenerArgumentCaptor.capture());
    // Simulate loading a preroll ad.
    AdsLoader.EventListener adsLoaderEventListener = eventListenerArgumentCaptor.getValue();
    adsLoaderEventListener.onAdPlaybackState(AD_PLAYBACK_STATE);
    shadowOf(Looper.getMainLooper()).idle();
}
Also used : FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MediaItem(androidx.media3.common.MediaItem) AdViewProvider(androidx.media3.common.AdViewProvider) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) EventListener(androidx.media3.exoplayer.source.ads.AdsLoader.EventListener) Before(org.junit.Before)

Example 3 with AdsLoader

use of androidx.media3.exoplayer.source.ads.AdsLoader in project media by androidx.

the class DefaultMediaSourceFactory method maybeWrapWithAdsMediaSource.

private MediaSource maybeWrapWithAdsMediaSource(MediaItem mediaItem, MediaSource mediaSource) {
    checkNotNull(mediaItem.localConfiguration);
    @Nullable MediaItem.AdsConfiguration adsConfiguration = mediaItem.localConfiguration.adsConfiguration;
    if (adsConfiguration == null) {
        return mediaSource;
    }
    @Nullable AdsLoader.Provider adsLoaderProvider = this.adsLoaderProvider;
    @Nullable AdViewProvider adViewProvider = this.adViewProvider;
    if (adsLoaderProvider == null || adViewProvider == null) {
        Log.w(TAG, "Playing media without ads. Configure ad support by calling setAdsLoaderProvider and" + " setAdViewProvider.");
        return mediaSource;
    }
    @Nullable AdsLoader adsLoader = adsLoaderProvider.getAdsLoader(adsConfiguration);
    if (adsLoader == null) {
        Log.w(TAG, "Playing media without ads, as no AdsLoader was provided.");
        return mediaSource;
    }
    return new AdsMediaSource(mediaSource, new DataSpec(adsConfiguration.adTagUri), /* adsId= */
    adsConfiguration.adsId != null ? adsConfiguration.adsId : ImmutableList.of(mediaItem.mediaId, mediaItem.localConfiguration.uri, adsConfiguration.adTagUri), /* adMediaSourceFactory= */
    this, adsLoader, adViewProvider);
}
Also used : MediaItem(androidx.media3.common.MediaItem) AdViewProvider(androidx.media3.common.AdViewProvider) AdsMediaSource(androidx.media3.exoplayer.source.ads.AdsMediaSource) DataSpec(androidx.media3.datasource.DataSpec) Nullable(androidx.annotation.Nullable) AdsLoader(androidx.media3.exoplayer.source.ads.AdsLoader)

Aggregations

AdViewProvider (androidx.media3.common.AdViewProvider)2 MediaItem (androidx.media3.common.MediaItem)2 Nullable (androidx.annotation.Nullable)1 AdPlaybackState (androidx.media3.common.AdPlaybackState)1 DataSpec (androidx.media3.datasource.DataSpec)1 MediaSource (androidx.media3.exoplayer.source.MediaSource)1 AdsLoader (androidx.media3.exoplayer.source.ads.AdsLoader)1 EventListener (androidx.media3.exoplayer.source.ads.AdsLoader.EventListener)1 AdsMediaSource (androidx.media3.exoplayer.source.ads.AdsMediaSource)1 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)1 AdsLoader (com.google.ads.interactivemedia.v3.api.AdsLoader)1 AdsRequest (com.google.ads.interactivemedia.v3.api.AdsRequest)1 IOException (java.io.IOException)1 Before (org.junit.Before)1