Skip to main content

插播广告

Step-1:Integration Reference#

  • Starting from V9.3.0.1, TP supports the in-stream video ad of Google Ad Manager(GAM).In-stream ads are also called in stream video ad, pre-roll ads, and generally appear before the video is played.
  • Ads play in a separate video player positioned on top of the app's content video player. Refer to the picture below:
  • Create an ad display container when requesting the ads.

NOTES#

  • Note: Don't perform the retry loading method ad in onAdFailed callback – it'll cause a lot of useless requests and could make your app run slowly.
  • TPMediaVideo should be in loadAd() or Start() in main thread.
  • The in-stream video will pause to play after your app is running background. So you have to control the video status through calling tpCustomMediaVideoAd.pause() in onPause() and tpCustomMediaVideoAd.resume() in onResume() if app switched between foreground and background.

Step-2:Load an ad#

  • Creating an ad object TPMediaVideo and posting the ad ID as a parameter.
  • Setting up the listener.
  • Posting the container you created as a parameter
  • Create a VideoAdPlayerAdapter class with the VideoView, and adapt it to IMA's VideoAdPlayer interface. This class will handle content and ad playback, and will contain the set of methods that a video player must implement to be used by the IMA SDK.For the specific implementation of VideoAdPlayerAdapter, please refer to Demo or IMA documents
private RelativeLayout mContainer;
private VideoAdPlayerAdapter videoAdPlayerAdpter;
...
TPMediaVideo tpMediaVideo = new TPMediaVideo(context,"advertising slot ID created on TP platform");
tpMediaVideo.setAdListener(new MediaVideoAdListener());
//It is recommended to use RelativeLayout, which makes it easier to center the advertising videoview
adContainer = new RelativeLayout(context);
//Container and VideoAdPlayerAdapter are mandatory parameters
tpMediaVideo.loadAd(mContainer,videoAdPlayerAdpter);

Step-3:Show the Streaming ad#

  • After the ad is loaded successfully, get the ad cached by the TP, and call the start() method to display the ad.
  • To confirm whether the ad has been loaded successfully, you can make use of 'onAdLoaded' or 'isReady()'.
tpMediaVideo.setAdListener(new MediaVideoAdListener() {
@Override
public void onAdLoaded(TPAdInfo tpAdInfo) {
// ad is loaded
ShowMeidaVideo();
}
});
...
private void ShowMeidaVideo() {
// obtain the ad from cache
TPCustomMediaVideoAd tpCustomMediaVideoAd = tpMediaVideo.getVideoAd();
// Starts playing the ad
tpCustomMediaVideoAd.start(null);
}

Step-4:Register Ad Event Callback#

  • DON'T request any ads in onAdFailed method. Oherwise you might encounter some invalid requests and UI lags in your app.
tpMediaVideo.setAdListener(new MediaVideoAdListener() {
@Override
public void onAdLoaded(TPAdInfo tpAdInfo) {
// ad is loaded succesfully
}
@Override
public void onAdFailed(TPAdError error) {
// ad is failed to load
}
@Override
public void onAdClicked(TPAdInfo tpAdInfo) {
// ad is clicked
}
@Override
public void onAdResume(TPAdInfo tpAdInfo) {
// resumes the current ad
}
@Override
public void onAdPause(TPAdInfo tpAdInfo) {
// pauses the current ad
}
@Override
public void onAdVideoStart(TPAdInfo tpAdInfo) {
// video ad starts playing
}
@Override
public void onAdVideoEnd(TPAdInfo tpAdInfo) {
// video ad playing to the end
}
@Override
public void onAdVideoError(TPAdInfo tpAdInfo, TPAdError error) {
// failed to play
}
@Override
public void onAdSkiped(TPAdInfo tpAdInfo) {
// skips the current ad
}
@Override
public void onAdTapped(TPAdInfo tpAdInfo) {
// ad is clicked out of the ad box
}
@Override
public void onAdProgress(TPAdInfo tpAdInfo, float progress, double totaltime) {
// playing progress
// duration
}
});
  • V9.7.10.1 Support new Listener
// Get the ad cache tpCustomMediaVideoAd call to get all the AdEvent events
tpCustomMediaVideoAd.setIMAEventListener(new TPMediaVideoAdapter.OnIMAEventListener() {
@Override
public void onEvent(Object adEvent) {
Log.i(TAG,"AdEvent = "+((AdEvent)adEvent).getType());
}
});

Step-5:Other APIs#

5.1 TPMediaVideo#

APIExplanation
isReady()Check for available ads.true-available
setCustomParams(Map<String, Object> map)customized key-value.Call it before loadAd.
onDestroy()Release TPMediaVideod object

5.2 TPCustomMediaVideoAd#

APIExplanation
pause()Pause the video.
resume()Resume playing.
setCustomShowData(Map<String,Object> customShowData)customized data,call it before ad displaying
getCustomNetworkObj()Obtain AdsManager object.AdsManager adsManager = (AdsManager)tpCustomMediaVideoAd.getCustomNetworkObj();
onDestroy()Release IMA object.

5.3 Hide the countdown timer#

  • Default:the countdown timer displayed.
Map<String, Object> mLocalExtras = new HashMap<>();
mLocalExtras.put("ima_ui_countdown",2); // 1 display;other-hide
tpMediaVideo.setCustomParams(mLocalExtras);
// loadAd
tpMediaVideo.loadAd(mContainer);

5.4 Sets the preferred language for the ad UI#

Map<String, Object> mLocalExtras = new HashMap<>();
mLocalExtras.put("ima_setting_language","en"); // e.g. en
tpMediaVideo.setCustomParams(mLocalExtras);
// loadAd
tpMediaVideo.loadAd(mContainer);