基本集成 (ExoPlayer AdsLoader API)

YandexAdsLoader 是一个简化的 API,用于通过 Media3 和 ExoPlayer 集成视频插播广告。YandexAdsLoader 支持播放三种类型的广告插入:Pre-roll、Mid-roll 和 Post-roll。

这种类型的集成适合不需要高级视频插播广告 API 功能的应用。

使用 Gradle 的集成

在 build.gradle 文件的应用级别添加以下依赖项:

dependencies {
          ...
          implementation 'com.yandex.android:mobileads:7.7.0'
          implementation 'androidx.media3:media3-exoplayer:1.4.1'
          implementation 'androidx.media3:media3-ui:1.4.1'
}
dependencies {
          ...
          implementation 'com.yandex.android:mobileads:7.7.0'
          implementation 'com.google.android.exoplayer:exoplayer-core:2.18.1'
}

广告呈现

  1. 将 PlayerView 添加到应用的布局中:

    <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>
    
  2. 使用 InstreamAdRequestConfiguration.Builder 类创建 instreamAdRequestConfiguration 请求配置。在请求参数中,传递来自 Yandex Advertising Network 合作伙伴界面的广告单元 ID (AD_UNIT_ID)。

    val configuration = InstreamAdRequestConfiguration
                .Builder(AD_UNIT_ID)
                .build()
    
    InstreamAdRequestConfiguration configuration = new InstreamAdRequestConfiguration
                .Builder(AD_UNIT_ID)
                .build();
    
  3. 使用 Activity 生命周期的onCreate() 方法创建 YandexAdsLoader 实例,以呈现和加载视频插播广告。

    备注

    要将 YandexAdsLoaderMedia3 一起使用,请从 com.yandex.mobile.ads.instream.media3 包中将其导入。要将其与 ExoPlayer2 一起使用,请从 com.yandex.mobile.ads.instream.exoplayer 包中将其导入。

    yandexAdsLoader = YandexAdsLoader(context, configuration)
    
    yandexAdsLoader = new YandexAdsLoader(context, configuration);
    
  4. 添加用于创建 ExoPlayer 的函数。在该函数内,创建一个 DefaultMediaSourceFactory 实例,并将 YandexAdsLoaderPlayerView 实例添加到其中。

    private 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;
    }
    
  5. 添加用于创建 MediaItem 的函数。

    private 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;
    }
    
  6. 添加用于初始化 ExoPlayer 和状态恢复的函数。

    private 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;
    }
    
  7. 添加用于调用 release() 方法并保存 ExoPlayer 状态的函数。

    private 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;
    }
    
  8. 使用您创建的函数重写 onStart()onResume()onPause()onStop() 方法。对于 API 版本 23 及更低版本,请在 onPause()onResume() 方法中调用创建的函数。在较新的 API 版本中,请从 onStop()onStart() 方法中调用它们。

    override 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();
        }
    }
    
  9. 当您不再需要 YandexAdsLoader 时,请在其上调用 release() 方法。

    override fun onDestroy() {
        releasePlayer()
        yandexAdsLoader.release()
        super.onDestroy()
    }
    
    @Override
    protected void onDestroy() {
        releasePlayer();
        yandexAdsLoader.release();
        super.onDestroy();
    }