The client-side ad insertion (CSAI) capability of ApsaraVideo Player SDK supports IMA SDK, providing a complete set of ad processing capabilities including ad media request, ad playback, and ad status upload. This topic describes how to use ApsaraVideo Player SDK for 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 to achieve seamless transition between ad playback and main content playback, while eliminating complex interactions between ad playback and IMA SDK.
AliyunPlayer supports two ad insertion methods:
Ad insertion playback based on a single VAST or VMAP adTagUri.
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 theadTagUrito 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 ad playback is completed or reaches the playback position, whether switching from ad to content or from content to ad, after implementing the "playback discontinuity" callback event (OnPositionDiscontinuityListener), the callback will notify the playback information before and after the switch.
During ad playback, most playback control or information retrieval APIs will take effect for ad playback.
Customized configuration to distinguish between ad and content playback
Currently, differential configuration for ads and content (such as PlayConfig, playback options, etc.) is supported. Related APIs can be set to: only effective for ad playback, only effective for content playback, or effective for all playback.
Key implementation for Android
Integrating the IMA plugin
The IMA plugin is an extension plugin supported by AliyunPlayer with the Google IMA SDK built in, so you do not need to integrate it separately. Currently, Maven integration is supported. You need to add dependencies in the project's build.gradle file. Example:
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
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);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) { } });Configure ad sources. There are two configuration methods:
NoteThe following two methods are mutually exclusive. You cannot use both
setSingleAdTagUriandaddAdItemssimultaneously.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);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);
Start playback.
//setAutoPlay when player prepared mPlayerWithAds.setAutoPlay(true); mPlayerWithAds.prepare();