略過一段時間的廣告插播

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

重要

當有內部搜尋要略過廣告時,可能會在播放中稍微暫停。

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

  1. 延伸預設廣告原則選擇器以覆寫預設行為。

    /**
     * Custom ad policy selector.
     */
    public class CustomAdPolicySelector extends DefaultAdPolicySelector {
    
        /**
         * Default constructor.
         *
         * @param item Associated media player item.
         */
        public function CustomAdPolicySelector(item:MediaPlayerItem) {
            super(item);
    
            item.player.addEventListener(AdBreakPlaybackEvent.AD_BREAK_COMPLETED,
                                         onAdBreakCompleted);
        }
    
        override public function selectPolicyForAdBreak(adPolicyInfo:AdPolicyInfo):String {
            if (shouldPlayAds()) {
                return super.selectPolicyForAdBreak(adPolicyInfo);
            }
    
            return AdBreakPolicy.SKIP;
        }
    
        override public function selectAdBreaksToPlay(adPolicyInfo:AdPolicyInfo):Vector.<AdBreakTimelineItem> {
            if (shouldPlayAds()) {
                return super.selectAdBreaksToPlay(adPolicyInfo);
            }
    
            return new Vector.<AdBreakTimelineItem>();
        }
    
        override public function selectPolicyForSeekIntoAd(adPolicyInfo:AdPolicyInfo):String {
            if (shouldPlayAds()) {
                return super.selectPolicyForSeekIntoAd(adPolicyInfo);
            }
    
            return AdPolicy.SKIP_TO_NEXT_AD_IN_AD_BREAK;
        }
    
        private function onAdBreakCompleted(event:AdBreakPlaybackEvent):void {
            _lastAdBreakPlayedTime = new Date().valueOf();
        }
    
        private function shouldPlayAds():Boolean {
            var currentTime:Number = new Date().valueOf();
            return isNaN(_lastAdBreakPlayedTime)
                    ||  (currentTime - _lastAdBreakPlayedTime > _minimumInterval);
        }
    
        private var _lastAdBreakPlayedTime:Number = NaN;
        private var _minimumInterval:Number = 300000; // 5 minutes in milliseconds
    }
    
  2. 建立使用自訂選擇器的新廣告工廠。

    public class CustomAdPolicyContentFactory extends DefaultContentFactory {
    
        private var _adPolicySelector:CustomAdPolicySelector;
    
        /**
         * Default constructor.
         */
        public function CustomAdPolicyContentFactory() {
    
        }
    
        /**
        * @inheritDoc
        */
        override protected function doRetrieveAdPolicySelector(item:MediaPlayerItem):AdPolicySelector {
        if (!_adPolicySelector) {
            _adPolicySelector = new CustomAdPolicySelector(item);
        }
    
        return _adPolicySelector;
        }
    }
    
  3. 註冊要與MediaPlayer一起使用的新廣告工廠類別。

    var mediaResource:MediaResource =
      MediaResource.createFromUrl("https://example.org/stream.m3u8", null);
    var mediaPlayerItemConfig:MediaPlayerItemConfig =
      PSDKConfig.retrieveMediaPlayerItemConfig();
    mediaPlayerItemConfig.advertisingFactory = new CustomAdPolicyContentFactory();
    player.replaceCurrentResource(mediaResource, mediaPlayerItemConfig);
    

本頁內容