激励视频广告
Rewarded video ads
参考Demo
ReferenceDemo
1. 主要API
Main API
1.1 WMRewardAd
- com.windmill.sdk.reward.WMRewardAd
| 方法名 Method | 方法介绍 Method Introduction |
|---|---|
| WMRewardAd(Activity activity, WMRewardAdRequest request) | 构造方法。参数说明:activity(展示广告的 activity)、request(广告请求对象)。 Constructor. Parameter description: activity (activity to display ads), request (ad request object). |
| setRewardedAdListener(WMRewardAdListener rewardAdListener) | 回调监听。广告相关状态会通过 rewardAdListener 通知开发者。 Callback listener. Ad related status will be notified to the developer through rewardAdListener. |
| loadAd() | 拉取广告。 load ads. |
| isReady() | 广告是否准备好,未准备好广告将无法展示。 Whether the ad is ready. If it is not ready, the ad will not be displayed. |
| show(Activity activity, HashMap<String, String> options) | 展示广告。参数说明:activity(展示广告的 activity)、options(广告展示时的扩展参数,可选,可参考demo)。 Display ads. Parameter description: activity (activity to display ads), options (extended parameters for ad display, optional, refer to demo). |
| checkValidAdCaches() | 查询当前广告位的所有缓存信息的AdInfo对象、在广告加载成功后调用。 Query the AdInfo object of all cached information of the current ad slot and call it after the ad is loaded successfully. |
| destroy() | 销毁广告。 Destroy ads. |
1.2 WMRewardAdRequest
- com.windmill.sdk.reward.WMRewardAdRequest
| 方法名 Method | 方法介绍 Method Introduction |
|---|---|
| WMRewardAdRequest(String placementId, String userId, Map<String, Object> options) | 构造方法。参数说明:placementId(广告位Id)、userId(用户Id可选;如使用服务端回调,用户Id必填)、options(扩展参数,可传任意)。 Constructor. Parameter description: placementId (ad slot ID), userId (user ID is optional; if server callback is used, user ID is required), options (extended parameters, any parameters can be passed). |
1.3 WMRewardAdListener
- com.windmill.sdk.reward.WMRewardAdListener
| 方法名 Method | 方法介绍 Method Introduction |
|---|---|
| onVideoAdLoadSuccess(String placementId) | 广告成功加载。参数说明:placementId(广告位Id)。 The ad was loaded successfully. Parameter description: placementId (ad slot Id). |
| onVideoAdLoadError(WindMillError error, String placementId) | 广告加载失败。参数说明:error(报错信息,具体可看其内部code和message)、placementId(报错的广告位Id)。 Ad loading failed. Parameter description: error (error information, see its internal code and message for details), placementId (the ID of the ad position where the error was reported). |
| onVideoAdPlayStart(AdInfo adInfo) | 广告成功展示,媒体可在此记录曝光。参数说明:adInfo(广告信息,具体可看其内部成员变量)。 The advertisement is successfully displayed, and the media can record the exposure here. Parameter description: adInfo (advertising information, see its internal member variables for details). |
| onVideoAdPlayError(WindMillError error, String placementId) | 广告播放出错。参数说明:error(报错信息,具体可看其内部code和message)、placementId(报错的广告位Id)。 Ad playback error. Parameter description: error (error information, see its internal code and message for details), placementId (the ID of the ad position where the error was reported). |
| onVideoAdPlayEnd(AdInfo adInfo) | 广告播放结束。参数说明:adInfo(广告信息,具体可看其内部成员变量)。 Advertisement playback ends. Parameter description: adInfo (advertisement information, see its internal member variables for details). |
| onVideoAdClicked(AdInfo adInfo) | 广告被点击。参数说明:adInfo(广告信息,具体可看其内部成员变量)。 The ad was clicked. Parameter description: adInfo (ad information, see its internal member variables for details). |
| onVideoRewarded(AdInfo adInfo, WMRewardInfo rewardInfo) | 广告获取奖励。参数说明:adInfo(广告信息,具体可看其内部成员变量)、rewardInfo(奖励信息)。 Advertise to get rewards. Parameter description: adInfo (advertisement information, see its internal member variables for details), rewardInfo (reward information). |
| onVideoAdClosed(AdInfo adInfo) | 广告关闭。参数说明:adInfo(广告信息,具体可看其内部成员变量)。 Advertisement is turned off. Parameter description: adInfo (advertisement information, see its internal member variables for details). |
2. 接入代码示例
Code example
2.1 设置监听回调
Setting the listener callback
private WMRewardAd rewardVideoAd;
/**
* PLACEMENT_ID 必填
* USER_ID 可选,若使用服务端回调必填
* OPTIONS 自定义参数 可选
*/
WMRewardAdRequest request = new WMRewardAdRequest(PLACEMENT_ID, USER_ID, OPTIONS);
rewardVideoAd = new WMRewardAd(this, request);
rewardVideoAd.setRewardedAdListener(new WMRewardAdListener() {
@Override
public void onVideoAdLoadSuccess(String placementId) {
}
@Override
public void onVideoAdPlayEnd(AdInfo adInfo)) {
}
@Override
public void onVideoAdPlayStart(AdInfo adInfo) {
}
@Override
public void onVideoAdClicked(AdInfo adInfo) {
}
/**
* WMRewardInfo 激励内容
* placementId 广告位
*/
@Override
public void onVideoRewarded(AdInfo adInfo, WMRewardInfo rewardInfo) {
}
@Override
public void onVideoAdClosed(AdInfo adInfo) {
}
/**
* 加载广告错误回调
* WindMillError 激励视频错误内容
* placementId 广告位
*/
@Override
public void onVideoAdLoadError(final WindMillError error, final String placementId) {
}
/**
* 播放错误回调
* WindMillError 激励视频错误内容
* placementId 广告位
*/
@Override
public void onVideoAdPlayError(final WindMillError error, final String placementId) {
}
});2.2 激励视频广告加载
Loading rewarded video ads
/**
*同一个WMRewardAd不建议在广告playing中重复load
*同一个WMRewardAd在onVideoAdClosed中可以load下一次广告
*/
if (rewardVideoAd != null) {
rewardVideoAd.loadAd();
}2.3 激励视频广告播放
Incentive video ad playback
/**
* 收到onVideoAdLoadSuccess回调代表广告已ready
* option 自定义参数(可选)
*/
if (rewardVideoAd != null && rewardVideoAd.isReady()) {
//广告播放
HashMap option = new HashMap();
option.put(WMConstants.AD_SCENE_ID, "567");
option.put(WMConstants.AD_SCENE_DESC, "转盘抽奖");
rewardVideoAd.show(this, option);
}2.4 激励回调风控相关
Incentive callback risk control
在ToBid激励回调onVideoRewarded方法中获取rewardInfo对象的内部数据。
Get the internal data of the rewardInfo object in the onVideoRewarded method of the ToBid incentive callback.
- com.windmill.sdk.reward.WMRewardInfo
| 方法名 Method | 方法介绍 Method Introduction |
|---|---|
| isReward() | 是否奖励 Whether to reward |
| getCustomData() | customData数据目前只针对穿山甲渠道 customData data is currently only available for the Pangolin channel |
| getTrans_id() | ToBid请求的唯一ID。 Unique ID of the ToBid request. |
| getThird_trans_id() | 获取第三方平台的请求的唯一ID。目前支持优量汇、百度等渠道 Get the unique ID of the request from the third-party platform. Currently supports channels such as Youlianghui and Baidu |
| getUser_id() | 获取userID。 Get userID. |
穿山甲customData数据取值可参考下方代码。
For the value of CSJ customData, please refer to the code below.
/**
* 穿山甲激励回调
*/
@Override
public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
Map<String, Object> customData = new HashMap<>();
customData.put("rewardVerify", rewardVerify);
customData.put("errorCode", errorCode);
customData.put("errorMsg", errorMsg);
callVideoAdRewardWithData(rewardVerify, customData);
}2.5 激励视频广告预加载注意事项
Precautions for preloading rewarded video ads
- 需要在平台开启预加载功能。
The preloading function needs to be enabled on the platform.
- 开启预加载功能后,sdk内部会在广告播放中再次去请求广告,成功后会再次回调onVideoAdLoadSuccess。(相当于load一次广告会有两次onVideoAdLoadSuccess。ToBid_3.3.0版本及以后不再回调两次)
After the preloading function is enabled, the SDK will request the ad again during the ad playback, and onVideoAdLoadSuccess will be called back again if it succeeds. ( This is equivalent to loading an ad once and calling onVideoAdLoadSuccess twice. ToBid_3.3.0 and later versions will no longer call back twice )
- 开启预加载功能后,要注意全局使用同一个WMRewardAd对象。(new注意判null)
After enabling the preloading function, please note that the same WMRewardAd object is used globally. ( Note that new is null )
- 广告播放时可先判断isReady(),如果ready直接播放,否则可再次调用loadAd()。
When playing an ad, you can first check isReady(). If it is ready, play it directly. Otherwise, call loadAd() again.
private WMRewardAd mRewardAd;//全局对象
/**
* 加载广告
*/
private void loadAd() {
if (mRewardAd == null) {
mRewardAd = new WMRewardAd(this, new WMRewardAdRequest(placementId, userID, options));
}
mRewardAd.setRewardedAdListener(new WMRewardAdListener() {
@Override
public void onVideoAdLoadSuccess(final String placementId) {
}
@Override
public void onVideoAdPlayEnd(AdInfo adInfo) {
}
@Override
public void onVideoAdPlayStart(AdInfo adInfo) {
}
@Override
public void onVideoAdClicked(AdInfo adInfo) {
}
@Override
public void onVideoAdClosed(AdInfo adInfo) {
}
@Override
public void onVideoRewarded(AdInfo adInfo, final WMRewardInfo rewardInfo) {
}
@Override
public void onVideoAdLoadError(final WindMillError error, final String placementId) {
}
@Override
public void onVideoAdPlayError(final WindMillError error, final String placementId) {
}
});
mRewardAd.loadAd();
}