實作自訂內容解析器

您可以根據預設解析器實作您自己的內容解析器。

當TVSDK偵測到新商機時,它會重複透過註冊的內容解析器尋找能夠使用canResolve方法解決該商機的內容解析器。 選擇返回true的第一個用於解決業務機會。 如果沒有內容解析程式,則會略過該商機。 由於內容解析程式通常是非同步的,因此當程式完成時,內容解析程式負責通知TVSDK。

  • 內容解析程式會呼叫client.place,以指定TVSDK需要執行的時間軸作業(通常是廣告插播位置)。
  • 如果解析過程成功,內容解析器將調用client.notifyCompleted;如果過程失敗,則調用client.notifyFailed
  1. 建立自訂的業務機會解析程式。

    public class CustomResolver extends ContentResolver { 
    
        /** 
         * Default constructor. 
         */ 
        public function CustomResolver() { 
        } 
    
        override protected function doConfigure(item:MediaPlayerItem):void { 
            // here you can read any media stream characteristics which 
            // might help configure your content resolver like 
            // - the media player item configuration through the item.config 
            // - the media resource metadata through item.resource.metadata 
        } 
    
        /** 
         * @inheritDoc 
         */ 
        override protected function doCanResolve(opportunity:Opportunity):Boolean { 
            // check if the opportunity can be resolved by this resolver 
            // if yes return true, otherwise return false 
    
            return true; 
        } 
    
        /** 
         * @inheritDoc 
         */ 
        override protected function doResolve(opportunity:Opportunity):void { 
            // start the resolving process 
            // communicate with your custom ad server 
    
            // in this example we assume that: 
            // - if successful, onResolveCompleted method will be invoked 
            // - if failed, onResolveFailed method will be invoked 
        } 
    
        private function onResolveCompleted(response:*):void { 
            try { 
                var proposals:Vector.<TimelineOperation> = new Vector.<TimelineOperation>(); 
    
                // extract the timeline ad placement from the response 
                // and add them to the proposal vector 
                // - extract the ad break 
                // - calculate the placement ( can reuse the opportunity.placement ) 
                // var timelineOperation:AdBreakPlacement = new AdBreakPlacement(placement, adBreak); 
                // proposals.push(timelineOperation); 
    
                client.process(proposals); 
                client.notifyCompleted(_opportunity); 
            } catch (error:Error) { 
                onResolveFailed(error); 
            } 
        } 
    
        private function onResolveFailed(error:Error):void { 
    
            var errorMetadata:Metadata = new Metadata(); 
            MetadataUtils.serializeError(error, errorMetadata); 
    
            var mediaError:MediaError = new MediaError(NotificationCode.CONTENT_RESOLVING_ERROR, errorMetadata); 
            client.notifyFailed(_opportunity, mediaError); 
        } 
    
        ... 
    }
    
  2. 建立自訂內容工廠,使用自訂內容解析程式。

    例如:

    public class CustomContentFactory extends DefaultContentFactory { 
            ... 
    
            /** 
             * @inheritDoc 
             */ 
            override protected function  
              doRetrieveResolvers(item:MediaPlayerItem):Vector.<ContentResolver> { 
                var result:Vector.<ContentResolver> = new Vector.<ContentResolver>(); 
    
                var resource:MediaResource = item.resource; 
    
                if (resource.metadata != null) { 
                    if (resource.metadata.containsKey(DefaultMetadataKeys.AUDITUDE_METADATA_KEY)) { 
                        result.push(new AuditudeAdResolver()); 
                    } else if (resource.metadata.containsKey("custom-opportunity-detector")) { 
                        result.push(new CustomResolver()); 
                    } 
                } 
                return result; 
            } 
    }
    
  3. 註冊要播放的媒體串流的自訂內容工廠。

    例如:

    var mediaPlayerItemConfig:MediaPlayerItemConfig = new DefaultMediaPlayerItemConfig(); 
    mediaPlayerItemConfig.advertisingFactory = new CustomContentFactory(); 
    ... 
    
    var advertisingMetadata:AdvertisingMetadata = new AdvertisingMetadata(); 
    // set any parameter you need for custom ad resolver 
    // advertisingMetadata.setValue("customparam", "customvalue"); 
    
    var metadata:Metadata = new Metadata(); 
    metadata.setMetadata("custom-opportunity-detector", advertisingMetadata); 
    var mediaResource:MediaResource = MediaResource.createFromUrl(url, metadata);
    
    player.replaceCurrentResource(mediaResource, mediaPlayerItemConfig);
    

本頁內容