Search in sources :

Example 1 with AdsLoader

use of com.google.android.exoplayer2.source.ads.AdsLoader in project ExoPlayer by google.

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(com.google.android.exoplayer2.MediaItem) AdViewProvider(com.google.android.exoplayer2.ui.AdViewProvider) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Nullable(androidx.annotation.Nullable) AdsLoader(com.google.android.exoplayer2.source.ads.AdsLoader)

Example 2 with AdsLoader

use of com.google.android.exoplayer2.source.ads.AdsLoader in project ExoPlayer by google.

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(com.google.android.exoplayer2.testutil.FakeMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) AdViewProvider(com.google.android.exoplayer2.ui.AdViewProvider) EventListener(com.google.android.exoplayer2.source.ads.AdsLoader.EventListener) EventListener(com.google.android.exoplayer2.source.ads.AdsLoader.EventListener) Before(org.junit.Before)

Example 3 with AdsLoader

use of com.google.android.exoplayer2.source.ads.AdsLoader in project ExoPlayer by google.

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(com.google.android.exoplayer2.source.ads.AdPlaybackState) IOException(java.io.IOException) AdsLoader(com.google.ads.interactivemedia.v3.api.AdsLoader)

Aggregations

MediaItem (com.google.android.exoplayer2.MediaItem)2 AdViewProvider (com.google.android.exoplayer2.ui.AdViewProvider)2 Nullable (androidx.annotation.Nullable)1 AdsLoader (com.google.ads.interactivemedia.v3.api.AdsLoader)1 AdsRequest (com.google.ads.interactivemedia.v3.api.AdsRequest)1 MediaSource (com.google.android.exoplayer2.source.MediaSource)1 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)1 AdsLoader (com.google.android.exoplayer2.source.ads.AdsLoader)1 EventListener (com.google.android.exoplayer2.source.ads.AdsLoader.EventListener)1 AdsMediaSource (com.google.android.exoplayer2.source.ads.AdsMediaSource)1 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)1 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)1 IOException (java.io.IOException)1 Before (org.junit.Before)1