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
-
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 theInstreamAdRequestConfiguration.Builder
class. Pass thePage_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 createdYandexAdsLoader
instance andPlayerView
to it.KotlinJavaval 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);
-
Create an
ExoPlayer
instance and add the createdmediaSourceFactory
to it. -
Add the created
ExoPlayer
instance toPlayerView
andYandexAdsLoader
.KotlinJavaval 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);
-
Create a
MediaItem
instance and add a link to the video to be played andYandexAdsLoader.AD_TAG_URI
to it.KotlinJavaval 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();
-
Start playing the video with the created
MediaItem
instance.KotlinJavaplayer.apply { setMediaItem(mediaItem) prepare() playWhenReady = true }
mPlayer.setMediaItem(mediaItem); mPlayer.prepare(); mPlayer.setPlayWhenReady(true);