기본 연동(ExoPlayer AdsLoader API)
YandexAdsLoader는 Media3와 ExoPlayer로 인스트림 광고를 연동하는 단순화된 API입니다. Pre-roll, Mid-roll, Post-roll 유형의 광고 삽입 재생을 지원합니다.
고급 InStream API 기능이 필요 없는 앱에 적합한 연동 방식입니다.
Gradle로 연동
앱 수준 build.gradle 파일에 다음 종속 항목을 추가합니다.
dependencies {
...
implementation 'com.yandex.android:mobileads:7.18.5'
implementation 'androidx.media3:media3-exoplayer:1.4.1'
implementation 'androidx.media3:media3-ui:1.4.1'
}
dependencies {
...
implementation 'com.yandex.android:mobileads:7.18.5'
implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
}
광고 렌더링
-
Add PlayerView to your app's layout:
Media3ExoPlayer2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.media3.ui.PlayerView android:id="@+id/player_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" /> </FrameLayout><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 an
instreamAdRequestConfigurationrequest configuration using theInstreamAdRequestConfiguration.Builderclass. In the request parameters, pass the page ID (PAGE_ID) from the Yandex Advertising Network interface.KotlinJavaval configuration = InstreamAdRequestConfiguration .Builder(PAGE_ID) .build()InstreamAdRequestConfiguration configuration = new InstreamAdRequestConfiguration .Builder(PAGE_ID) .build(); -
Create a
YandexAdsLoaderinstance using theonCreate()method of theActivitycycle to render and load InStream ads.Примечание
To use
YandexAdsLoaderwithMedia3, import it from thecom.yandex.mobile.ads.instream.media3package. To use it withExoPlayer2, import it from thecom.yandex.mobile.ads.instream.exoplayerpackage.KotlinJavayandexAdsLoader = YandexAdsLoader(context, configuration)yandexAdsLoader = new YandexAdsLoader(context, configuration); -
Add a function for creating
ExoPlayer. Inside the function, create aDefaultMediaSourceFactoryinstance and add to it yourYandexAdsLoaderandPlayerViewinstances.KotlinJavaprivate fun createPlayer(): Player { val mediaSourceFactory = DefaultMediaSourceFactory(context) .setLocalAdInsertionComponents({ yandexAdsLoader }, playerView) val player = ExoPlayer.Builder(context) .setMediaSourceFactory(mediaSourceFactory) .build() return player }private YandexAdsLoader yandexAdsLoader; ... private class YandexAdsLoaderProvider implements AdsLoader.Provider { @Nullable @Override public AdsLoader getAdsLoader(@NonNull MediaItem.AdsConfiguration adsConfiguration) { return yandexAdsLoader; } } private Player createPlayer() { AdsLoader.Provider yandexAdsLoaderProvider = new YandexAdsLoaderProvider(); DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(context) .setLocalAdInsertionComponents(yandexAdsLoaderProvider, playerView); Player player = new ExoPlayer.Builder(context) .setMediaSourceFactory(mediaSourceFactory) .build(); return player; } -
Add a function for creating
MediaItem.KotlinJavaprivate fun getMediaItem(): MediaItem { val contentVideoUrl = CONTENT_URL val adTagUri = Uri.parse(YandexAdsLoader.AD_TAG_URI) val mediaItem = MediaItem.Builder() .setUri(contentVideoUrl) .setAdsConfiguration(MediaItem.AdsConfiguration.Builder(adTagUri).build()) .build() return mediaItem }private MediaItem getMediaItem() { String contentVideoUrl = CONTENT_URL; Uri adTagUri = Uri.parse(YandexAdsLoader.AD_TAG_URI); MediaItem mediaItem = new MediaItem.Builder() .setUri(contentVideoUrl) .setAdsConfiguration(new MediaItem.AdsConfiguration.Builder(adTagUri).build()) .build(); return mediaItem; } -
Add a function for initializing
ExoPlayerand state recovery.KotlinJavaprivate var rememberedPlayerPosition = 0L private var rememberedPlayWhenReady = false private var player: Player? = null ... private fun initPlayer() { val mediaItem = getMediaItem() player = createPlayer().also { playerView.player = it yandexAdsLoader.setPlayer(it) it.setMediaItem(mediaItem) it.playWhenReady = rememberedPlayWhenReady it.seekTo(rememberedPlayerPosition) it.prepare() } }private Long rememberedPlayerPosition = 0L; private Boolean rememberedPlayWhenReady = false; private Player player = null; ... private void initPlayer() { MediaItem mediaItem = getMediaItem(); Player player = createPlayer(); playerView.setPlayer(player); yandexAdsLoader.setPlayer(player); player.setMediaItem(mediaItem); player.setPlayWhenReady(rememberedPlayWhenReady); player.seekTo(rememberedPlayerPosition); player.prepare(); this.player = player; } -
Add a function for calling the
release()method and saving theExoPlayerstate.KotlinJavaprivate var rememberedPlayerPosition = 0L private var rememberedPlayWhenReady = false private var player: Player? = null ... private fun releasePlayer() { yandexAdsLoader.setPlayer(null) playerView.player = null player?.also { rememberedPlayerPosition = it.contentPosition rememberedPlayWhenReady = it.playWhenReady it.release() } player = null }private Long rememberedPlayerPosition = 0L; private Boolean rememberedPlayWhenReady = false; private Player player = null; ... private void releasePlayer() { yandexAdsLoader.setPlayer(null); playerView.setPlayer(null); Player player = this.player; if (player != null) { rememberedPlayerPosition = player.getContentPosition(); rememberedPlayWhenReady = player.getPlayWhenReady(); player.release(); } this.player = null; } -
Rewrite the
onStart(),onResume(),onPause(), andonStop()methods using the functions you created. For theAPIversion 23 and lower, call the created functions in theonPause()andonResume()methods. In newerAPIversions, call them from theonStop()andonStart()methods.KotlinJavaoverride fun onStart() { super.onStart() if (Build.VERSION.SDK_INT > 23) { initPlayer() playerView.onResume() } } override fun onStop() { super.onStop() if (Build.VERSION.SDK_INT > 23) { playerView.onPause() releasePlayer() } } override fun onResume() { super.onResume() if (Build.VERSION.SDK_INT <= 23) { initPlayer() playerView.onResume() } } override fun onPause() { super.onPause() if (Build.VERSION.SDK_INT <= 23) { playerView.onPause() releasePlayer() } }@Override protected void onStart() { super.onStart(); if (Build.VERSION.SDK_INT > 23) { initPlayer(); playerView.onResume(); } } @Override protected void onStop() { super.onStop(); if (Build.VERSION.SDK_INT > 23) { playerView.onPause(); releasePlayer(); } } @Override protected void onResume() { super.onResume(); if (Build.VERSION.SDK_INT <= 23) { initPlayer(); playerView.onResume(); } } @Override protected void onPause() { super.onPause(); if (Build.VERSION.SDK_INT <= 23) { playerView.onPause(); releasePlayer(); } } -
Call the
release()method onYandexAdsLoaderwhen you no longer need it.KotlinJavaoverride fun onDestroy() { releasePlayer() yandexAdsLoader.release() super.onDestroy() }@Override protected void onDestroy() { releasePlayer(); yandexAdsLoader.release(); super.onDestroy(); }