在期間略過廣告插播

依預設,當使用者搜尋廣告插播時,TVSDK會強制播放廣告插播。 如果從先前的中斷完成所經過的時間在特定分鐘內,您可以自訂跳過廣告中斷的行為。

重要

如果您必須完成內部尋求以原諒廣告,在播放期間可能會有輕微的暫停。

若要覆寫預設的TVSDK廣告插播行為,您可以延伸預設廣告政策選擇器。 有四種廣告插播政策可供使用:

  • 播放

  • 略過

    注意

    當廣告出現在即時點時,SKIP廣告中斷政策可能無法如預期般適用於即時串流。 例如,對於前段,SKIP會造成廣告中斷結尾的搜尋,其可能大於即時點。 在此情況下,TVSDK可能會尋找廣告的中間位置。

  • REMOVE_AFTER

  • 移除

    注意

    REMOVE廣告插播政策將被淘汰。 Adobe建議您使用SKIP廣告插播策略來取代REMOVE

下列自訂廣告政策選擇器範例會在使用者觀看廣告插播後的5分鐘(塗鴉牆時鐘時間)跳過廣告。

  1. 當使用者觀看完廣告插播時,節省目前的系統時間。

    @Override
    public void onAdBreakComplete(AdBreak adBreak) {
        ...
        if (isShouldPlayUpcomingAdBreakRuleEnabled()) {
            CustomAdPolicySelector.setLastAdBreakPlayedTime(System.currentTimeMillis());
            ...
        }
    }
    
  2. 延伸AdPolicySelector

    package com.adobe.mediacore.sample.advertising;
    
    import com.adobe.mediacore.MediaPlayerItem;
    import com.adobe.mediacore.MediaPlayerItemConfig;
    import com.adobe.mediacore.timeline.advertising.policy.*;
    import com.adobe.mediacore.timeline.advertising.AdBreakTimelineItem;
    import com.adobe.mediacore.metadata.AdvertisingMetadata;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class CustomAdPolicySelector implements AdPolicySelector {
    
        private static final long MIN_BREAK_INTERVAL = 300000; // 5 minutes for next ad break to be played
        private MediaPlayerItem _mediaPlayerItem;
        private static long _lastAdBreakPlayedTime;
        private AdBreakWatchedPolicy watchedPolicy = AdBreakWatchedPolicy.WATCHED_ON_BEGIN;
    
        public CustomAdPolicySelector(MediaPlayerItem mediaPlayerItem) {
            _mediaPlayerItem = mediaPlayerItem;
            _lastAdBreakPlayedTime = 0;
    
            if (mediaPlayerItem != null) {
                watchedPolicy = extractWatchedPolicy(mediaPlayerItem.getConfig());
            }
        }
    
        @Override
        public AdBreakPolicy selectPolicyForAdBreak(AdPolicyInfo adPolicyInfo) {
            if (shouldPlayAdBreaks() && adPolicyInfo.getAdBreakTimelineItems() != null) {
    
                AdBreakTimelineItem item = adPolicyInfo.getAdBreakTimelineItems().get(0);
    
                // This condition will remove the pre-roll ad from live stream after watching
                if (item.getTime() == 0 && _mediaPlayerItem.isLive()) {
                    return AdBreakPolicy.REMOVE_AFTER_PLAY;
                }
                if (item.getTime() == 0) {
                    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 (item.isWatched()) {
                    return AdBreakPolicy.SKIP;
                }
    
                return AdBreakPolicy.REMOVE_AFTER_PLAY;
            }
    
            return AdBreakPolicy.SKIP;
        }
    
        @Override
        public List<AdBreakTimelineItem> selectAdBreaksToPlay(AdPolicyInfo adPolicyInfo) {
    
            if (shouldPlayAdBreaks()) {
    
                List<AdBreakTimelineItem> timelineItems = adPolicyInfo.getAdBreakTimelineItems();
                AdBreakTimelineItem item;
                List<AdBreakTimelineItem> selectedItems = new ArrayList<AdBreakTimelineItem>();
    
                if (timelineItems != null && timelineItems.size() > 0) {
    
                    // Seek Forward Condition
                    if (adPolicyInfo.getCurrentTime() <= adPolicyInfo.getSeekToTime()) {
                        item = timelineItems.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(item.getTime() == 0 && !item.isWatched()) {
                                // comment this line if you just need to seek to the user's
                                // last known position without playing pre-roll ad. ZD#820
                                selectedItems.add(item);
                                return selectedItems;
                            }
                            else{
                                return null;
                            }
                        } else {
                            item = timelineItems.get(timelineItems.size()-1);
                            if (!item.isWatched()) {
                                selectedItems.add(item);
                                return selectedItems;
                            }
                        }
    
                        // Seek backward condition
                    } else if (adPolicyInfo.getCurrentTime() > adPolicyInfo.getSeekToTime()) {
                        item = timelineItems.get(0);
    
                        if(!item.isWatched()) {
                            selectedItems.add(item);
                            return selectedItems;
                        } else {
                            return null;
                        }
                    }
                }
            }
            return null;
        }
    
        @Override
        public AdPolicy selectPolicyForSeekIntoAd(AdPolicyInfo adPolicyInfo) {
            // Simple Ad Policy selector
            // if the first ad in the break was watched,
            // skip to the next add after the seek position
            // otherwise, play the ads in the break from the beginning
    
            List<AdBreakTimelineItem> timelineItems = adPolicyInfo.getAdBreakTimelineItems();
            if (timelineItems != null && timelineItems.size() > 0) {
                if (timelineItems.get(0).isWatched()) {
                    return AdPolicy.SKIP_TO_NEXT_AD_IN_AD_BREAK;
                }
            }
    
            // Resume play from the next ad in the break
            return AdPolicy.PLAY_FROM_AD_BREAK_BEGIN;
    
        }
    
        @Override
        public AdBreakWatchedPolicy selectWatchedPolicyForAdBreak(AdPolicyInfo adPolicyInfo) {
            return watchedPolicy;
        }
    
        public static void setLastAdBreakPlayedTime(long lastAdBreakPlayedTime) {
            _lastAdBreakPlayedTime = lastAdBreakPlayedTime;
        }
    
        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;
        }
    
        private AdBreakWatchedPolicy extractWatchedPolicy(MediaPlayerItemConfig config) {
            if (config != null) {
                AdvertisingMetadata metadata = config.getAdvertisingMetadata();
                if (metadata != null) {
                    return metadata.getAdBreakWatchedPolicy();
                }
            }
            return AdBreakWatchedPolicy.WATCHED_ON_BEGIN;
        }
    }
    

本頁內容