Default PSDK behavior is to force an ad break to play when the user seeks over an ad break. You could customize the behavior to skip an ad break if the time elapsed from a previous break completion is within a certain number of minutes.
The following example of a customized ad policy selector skips any ads within the next five minutes (wall clock time) after a user has watched an ad break.
@Override
public void onAdBreakComplete(AdBreak adBreak) {
...
if (isShouldPlayUpcomingAdBreakRuleEnabled())
CustomAdPolicySelector.setLastAdBreakPlayedTime(System.currentTimeMillis());
...
}
package com.adobe.mediacore.sample.advertising;
import com.adobe.mediacore.DefaultAdPolicySelector;
import com.adobe.mediacore.MediaPlayerItem;
import com.adobe.mediacore.timeline.advertising.AdBreakPlacement;
import com.adobe.mediacore.timeline.advertising.AdBreakPolicy;
import com.adobe.mediacore.timeline.advertising.AdPolicyInfo;
import com.adobe.mediacore.timeline.advertising.PlacementInformation;
import java.util.ArrayList;
import java.util.List;
public class CustomAdPolicySelector extends DefaultAdPolicySelector {
private final long MIN_BREAK_INTERVAL = 300000; // 5 minutes interval for next ad break to be played
private MediaPlayerItem _mediaPlayerItem;
private static long _lastAdBreakPlayedTime;
public CustomAdPolicySelector(MediaPlayerItem mediaPlayerItem) {
super(mediaPlayerItem);
_mediaPlayerItem = mediaPlayerItem;
_lastAdBreakPlayedTime = 0;
}
@Override
public AdBreakPolicy selectPolicyForAdBreak(AdPolicyInfo adPolicyInfo) {
if (shouldPlayAdBreaks()) {
AdBreakPlacement adBreakPlacement = adPolicyInfo.getAdBreakPlacements().get(0);
// This condition will remove the pre-roll ad from live stream after watching
if (adBreakPlacement.getPlacement().getType().equals(PlacementInformation.Type.PRE_ROLL) && _mediaPlayerItem.isLive()) {
return AdBreakPolicy.REMOVE_AFTER_PLAY;
}
if (adBreakPlacement.getPlacement().getType().equals(PlacementInformation.Type.PRE_ROLL)) {
return AdBreakPolicy.PLAY;
}
// This condition will remove every ad break that has been watched once. Comment this section if you want to play watched ad breaks again.
if (adBreakPlacement.getAdBreak().isWatched()) {
return AdBreakPolicy.SKIP;
}
return AdBreakPolicy.REMOVE_AFTER_PLAY;
}
return AdBreakPolicy.SKIP;
}
@Override
public List<AdBreakPlacement> selectAdBreaksToPlay(AdPolicyInfo adPolicyInfo) {
if (shouldPlayAdBreaks()) {
List<AdBreakPlacement> adBreakPlacements = adPolicyInfo.getAdBreakPlacements();
if (adBreakPlacements != null) {
int size = adBreakPlacements.size();
// Seek Forward Condition
if (size > 0 && adPolicyInfo.getCurrentTime() <= adPolicyInfo.getSeekToTime()) {
List<AdBreakPlacement> adBreaks = new ArrayList<AdBreakPlacement>();
AdBreakPlacement adBreakPlacement = adBreakPlacements.get(0);
// Resume logic - This will be helpful in resuming the content from last saved playback session, and just play the pre-roll ad
if(adPolicyInfo.getCurrentTime() == 0){
if(adBreakPlacement.getPlacement().getType().equals(PlacementInformation.Type.PRE_ROLL) && !adBreakPlacement.getAdBreak().isWatched()) {
//comment this line if you just need to seek to the user's last known position without playing pre-roll ad. ZD#820
adBreaks.add(adBreakPlacement);
return adBreaks;
} else{
return null;
}
} else {
adBreakPlacement = adBreakPlacements.get(size -1);
if (!adBreakPlacement.getAdBreak().isWatched()) {
adBreaks.add(adBreakPlacement);
return adBreaks;
}
}
// Seek backward condition
} else if (size > 0 && adPolicyInfo.getCurrentTime() > adPolicyInfo.getSeekToTime()) {
List<AdBreakPlacement> adBreaks = new ArrayList<AdBreakPlacement>();
AdBreakPlacement adBreakPlacement = adBreakPlacements.get(0);
if(!adBreakPlacement.getAdBreak().isWatched()) {
adBreaks.add(adBreakPlacement);
return adBreaks;
} else {
return adBreaks;
}
}
}
}
return null;
}
public static void setLastAdBreakPlayedTime(long lastAdBreakPlayedTime) {
_lastAdBreakPlayedTime = lastAdBreakPlayedTime;
}
// used to decide if upcoming AdBreak should be played or not, Customer use case #2 - Five minute AdBreak rule
private boolean shouldPlayAdBreaks(){
long currentTime = System.currentTimeMillis();
if (_lastAdBreakPlayedTime <= 0) {
return true;
}
if (_lastAdBreakPlayedTime > 0 && (currentTime - _lastAdBreakPlayedTime) > MIN_BREAK_INTERVAL) {
return true;
}
// return false for not playing Ad if this Ad occurs with 5 minutes of last Ad playback
return false;
}
}