Basic integration (ExoPlayer AdsLoader API)
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.
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
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>
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.Create an instance of the YandexAdsLoader class to serve and upload InStream ads.
Create an instance of the
DefaultMediaSourceFactory
class and add the created YandexAdsLoader instance andPlayerView
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)
Create an
ExoPlayer
instance and add the createdmediaSourceFactory
to it.Add the created
ExoPlayer
instance toPlayerView
and YandexAdsLoader.val player = SimpleExoPlayer.Builder(this) .setMediaSourceFactory(mediaSourceFactory).build() playerView.player = player yandexAdsLoader.setPlayer(player)
Create a
MediaItem
instance and add a link to the video to be played andYandexAdsLoader.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()
Start playing the video with the created
MediaItem
instance.player.apply { setMediaItem(mediaItem) prepare() playWhenReady = true }