Basic integration (ExoPlayer AdsLoader API)

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

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.

  1. Enable using Gradle
  2. Rendering ads

Enable using Gradle

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

dependencies {
    ...
    implementation 'com.yandex.android:mobileads:5.10.0'
    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 identifier (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)
  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)
  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()
  8. Start playing the video with the created MediaItem instance.

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