Basic integration (ExoPlayer AdsLoader API)

YandexAdsLoader is a simplified API for integrating InStream ads using ExoPlayer. YandexAdsLoader supports playing Pre-roll, Mid-roll, and Post-roll ad breaks.

This type of integration is suitable for apps that do not require advanced InStream API features.

Enable using Gradle

Add the following dependencies to the build.gradle file at the application level:

dependencies {
    ...
    implementation 'com.yandex.android:mobileads:7.0.1'
    implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
}

Rendering ads

  1. Add com.google.android.exoplayer2.ui.PlayerView to the app layout.

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/player_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    
    </FrameLayout>
    
  2. Create a configuration for the instreamAdRequestConfiguration request using the InstreamAdRequestConfiguration.Builder class. Pass the Page_ID from the partner interface as a request parameter.

  3. Create an instance of the YandexAdsLoader class to serve and upload InStream ads.

  4. Create an instance of the DefaultMediaSourceFactory class and add the created YandexAdsLoader instance and PlayerView to it.

    val userAgent = Util.getUserAgent(this, getString(R.string.app_name))
    val dataSourceFactory = DefaultDataSourceFactory(this, userAgent)
    val mediaSourceFactory = DefaultMediaSourceFactory(dataSourceFactory)
        .setAdsLoaderProvider { yandexAdsLoader }
        .setAdViewProvider(playerView)
    
    final String userAgent = Util.getUserAgent(this, getString(R.string.app_name));
    final DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, userAgent);
    final DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(dataSourceFactory);
    mediaSourceFactory.setAdsLoaderProvider(mYandexAdsLoader);
    mediaSourceFactory.setAdViewProvider(mBinding.playerView);
    
  5. Create an ExoPlayer instance and add the created mediaSourceFactory to it.

  6. Add the created ExoPlayer instance to PlayerView and YandexAdsLoader.

    val player = SimpleExoPlayer.Builder(this)
        .setMediaSourceFactory(mediaSourceFactory).build()
    playerView.player = player
    yandexAdsLoader.setPlayer(player)
    
    final SimpleExoPlayer player = new SimpleExoPlayer.Builder(this)
            .setMediaSourceFactory(mediaSourceFactory)
            .build();
    mPlayerView.setPlayer(player);
    mYandexAdsLoader.setPlayer(player);
    
  7. Create a MediaItem instance and add a link to the video to be played and YandexAdsLoader.AD_TAG_URI to it.

    val contentVideoUrl = getString(R.string.content_url_for_instream_ad)
    val mediaItem = MediaItem.Builder()
        .setUri(contentVideoUrl)
        .setAdTagUri(YandexAdsLoader.AD_TAG_URI).build()
    
    final String contentVideoUrl = getString(R.string.content_url_for_instream_ad);
    final MediaItem mediaItem = new MediaItem.Builder()
            .setUri(contentVideoUrl)
            .setAdTagUri(YandexAdsLoader.AD_TAG_URI)
            .build();
    
  8. Start playing the video with the created MediaItem instance.

    player.apply {
        setMediaItem(mediaItem)
        prepare()
        playWhenReady = true
    }
    
    mPlayer.setMediaItem(mediaItem);
    mPlayer.prepare();
    mPlayer.setPlayWhenReady(true);