How to implement client-side ad insertion playback

更新时间:
复制 MD 格式

ApsaraVideo Player SDK supports client-side ad insertion (CSAI) through the IMA SDK, handling ad media requests, ad playback, and ad status reporting. This guide walks you through setting up ad insertion playback on Android.

Limits

  • Currently only the Android platform is supported.

  • Supported ApsaraVideo Player SDK version for Android: com.aliyun.sdk.android:AliyunPlayer:7.4.0-full-46755290.

  • Supported AlivcImaExtension SDK version for Android: com.aliyun.sdk.android:AlivcImaExtension:7.4.0-46755290.

  • Ad insertion during live streaming is not supported.

Prerequisites

  • Integrate ApsaraVideo Player SDK with the specified version above. For more information, see Quick Intergation.

Overview

ApsaraVideo Player SDK provides multiple linear ad insertion methods for seamless transitions between ads and main content, while abstracting away the complexity of IMA SDK interactions.

AliyunPlayer supports two ad insertion methods:

  1. Ad insertion playback based on a single VAST or VMAP adTagUri.

  2. Ad insertion playback based on single or multiple custom playback positions.

Ad request logic

  • Ad insertion playback with a single adTagUri: When playback starts, the system first requests the adTagUri to obtain ad positions. For VAST requests, the ad is typically played as a pre-roll ad.

  • Ad insertion playback with custom playback positions: Ad requests are initiated before playback reaches the specified position.

Ad/content switching playback logic

When switching between ads and content in either direction, the OnPositionDiscontinuityListener callback notifies you of the playback state before and after the transition.

During ad playback, most playback control and information retrieval APIs apply to the ad rather than the main content.

Customized configuration to distinguish between ad and content playback

You can apply different configurations (such as PlayConfig and playback options) to ad playback only, content playback only, or both.

Key implementation for Android

Integrating the IMA plugin

The IMA plugin bundles the Google IMA SDK, so you do not need to integrate it separately. Add the following dependencies to your project's build.gradle file:

def player_ima_sdk_extension_version = "7.4.0-46755290" // player ima extension sdk current version
def player_sdk_version = "7.4.0-full-46755290" //player sdk current version that support ads


//ImaExtension 
implementation "com.aliyun.sdk.android:AlivcImaExtension:$player_ima_sdk_extension_version"

//player sdk
implementation "com.aliyun.sdk.android:AliyunPlayer:$player_sdk_version"

Setting up ad playback

  1. Initialization (ad player, loader).

    //initial adsLoader, may choose start debug mode
    AdsLoader mAdsLoader = new ImaAdsLoader.Builder(getApplicationContext()).setDebugModeEnabled(false).build();
    
    //initial AliPlayerWithAds, to locate problem online, inputing traceId is recommended.
    AliPlayerWithAds mPlayerWithAds = AliPlayerFactory.createAliPlayerWithAds(getApplicationContext(), "your_traceId", mAdsLoader);
    
    //bind the player with adsloader
    mAdsLoader.setPlayer(mPlayerWithAds);
    
    
    //optional, set mMaxBufferDuration to 20s only to content player
    PlayerConfig config = mPlayerWithAds.getConfig(IPlayerWithAds.MethodEffectType.Effect_Only_Content);
    config.mMaxBufferDuration = 20000;
    mPlayerWithAds.setConfig(config, IPlayerWithAds.MethodEffectType.Effect_Only_Content);
  2. Configure listeners.

    //onPositionDiscontinuityListener, callback when ad/content transplayback
    IPlayerWithAds.OnPositionDiscontinuityListener onPositionDiscontinuityListener = new IPlayerWithAds.OnPositionDiscontinuityListener() {
        @Override
        public void onPositionDiscontinuity(PositionInfo oldInfo, PositionInfo newInfo) {
            
        }
    };
    mPlayerWithAds.setOnPositionDiscontinuityListener(onPositionDiscontinuityListener);
    
    
    //onPlaybackListReadyListener, callback when playlist ready
    IPlayerWithAds.OnPlaybackListReadyListener onPlaybackListReadyListener = new IPlayerWithAds.OnPlaybackListReadyListener() {
        @Override
        public void onPlaybackListReady(long contentPositionMs, long contentDurationMs, AdGroupBean[] adGroupBeans) {
    
        }
    };
    mPlayerWithAds.setOnPlaybackListReadyListener(onPlaybackListReadyListener);
    
    
    //set content preparedListener
    mPlayerWithAds.setOnPreparedListener(new IPlayer.OnPreparedListener() {
        @Override
        public void onPrepared() {
        }
    }, IPlayerWithAds.MethodEffectType.Effect_Only_Content);
    
    
    //set firstRender listener, callback when content or ad first Render
    //maybe callback multi times
    mPlayerWithAds.setOnRenderingStartListener(new IPlayer.OnRenderingStartListener() {
        @Override
        public void onRenderingStart() {
    
        }
    }, IPlayerWithAds.MethodEffectType.Effect_WhatEver);
    
    
    //SurfaceTextureListener, setSurface to player when surface ready 
    TextureView mTextureView = findViewById(R.id.surface_view);
    mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
            mSurface = new Surface(surface);
            mPlayerWithAds.setSurface(mSurface);
        }
    
        @Override
        public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
        }
        @Override
        public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
            return false;
        }
        @Override
        public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
        }
    });
  3. Configure ad sources. There are two configuration methods:

    Note

    The following two methods are mutually exclusive. You cannot use both setSingleAdTagUri and addAdItems simultaneously.

    1. Ad insertion playback based on a single VAST or VMAP adTagUri.

      String mediaUri = "https://player.alicdn.com/video/aliyunmedia.mp4";
      
      //single inline adTagUri(vast)
      String adTagUri = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=";
      
      AdsUrlSource adsUrlSource = new AdsUrlSource();
      adsUrlSource.setUri(mediaUri);
      adsUrlSource.setSingleAdTagUri(adTagUri);
      
      mPlayerWithAds.setDataSource(adsUrlSource);
      
    2. Ad insertion playback based on single or multiple custom playback positions.

      String mediaUri = "https://player.alicdn.com/video/aliyunmedia.mp4";
      
      //single inline adTagUri(vast), not support vmap adtaguri to this adItem 
      String singleInlineVastAdTagUri = "https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/single_ad_samples&ciu_szs=300x250&impl=s&gdfp_req=1&env=vp&output=vast&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ct%3Dlinear&correlator=";
      
      AdsUrlSource adsUrlSource = new AdsUrlSource();
      adsUrlSource.setUri(mediaUri);
      
      AdsSourceBase.AdItem preItem = new AdsSourceBase.AdItem(0, singleInlineVastAdTagUri);
      AdsSourceBase.AdItem midItem15s = new AdsSourceBase.AdItem(15 * 1000, singleInlineVastAdTagUri);
      AdsSourceBase.AdItem midItem50Percent = new AdsSourceBase.AdItem("50%", singleInlineVastAdTagUri);
      AdsSourceBase.AdItem postItem = new AdsSourceBase.AdItem("post", singleInlineVastAdTagUri);
      
      adsUrlSource.addAdItems(preItem, midItem15s, midItem50Percent, postItem);
  4. Start playback.

    //setAutoPlay when player prepared
    mPlayerWithAds.setAutoPlay(true);
    
    mPlayerWithAds.prepare();