實作自訂內容解析器

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

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

  1. 建立自訂AdvertisingFactory例項並覆寫createContentResolver

    例如:

    new AdvertisingFactory() { 
        ... 
        @Override 
        public ContentResolver createContentResolver(MediaPlayerItem item) { 
            Metadata metadata = _mediaPlayer.getCurrentItem().getResource().getMetadata(); 
            if (metadata != null) { 
                if (metadata.containsKey(DefaultMetadataKeys.AUDITUDE_METADATA_KEY.getValue())) { 
                    return new AuditudeResolver(getActivity().getApplicationContext()); 
                } else if (metadata.containsKey(DefaultMetadataKeys.JSON_METADATA_KEY.getValue())) { 
                    return new MetadataResolver(); 
                } else if (metadata.containsKey(DefaultMetadataKeys.TIME_RANGES_METADATA_KEY.getValue())) { 
                    return new CustomAdMarkersContentResolver(); 
                } else if (metadata.containsKey(CustomAdResolver.CUSTOM_METADATA_KEY)) { 
                    return new CustomAdResolver(); 
                } 
            } 
            return null; 
        } 
        ... 
    }
    
  2. MediaPlayer註冊廣告客戶端工廠。

    例如:

    // register the custom advertising factory with media player 
    advertisingFactory = createCustomAdvertisingFactory(); 
    mediaPlayer.registerAdClientFactory(advertisingFactory);
    
  3. AdvertisingMetadata物件傳遞至TVSDK,如下所示:

    1. 建立AdvertisingMetadata對象和MetadataNode對象。
    2. AdvertisingMetadata對象保存到MetadataNode
    MetadataNode result = new MetadataNode(); 
    result.setNode(DefaultMetadataKeys.ADVERTISING_METADATA.getValue(),  
                   advertisingMetadata);
    
  4. 建立可擴充ContentResolver類別的自訂廣告解析程式類別。

    1. 在自訂廣告解析程式中,覆寫此受保護的函式:

      void doResolveAds(Metadata metadata,  
                        PlacementOpportunity placementOpportunity)
      

      中繼資料包含您的AdvertisingMetada。 用於以下TimelineOperation向量生成。

    2. 對於每個職位安排機會,建立一個Vector<TimelineOperation>

      向量可以是空的,但不是空的。

      此範例TimelineOperation提供AdBreakPlacement的結構:

      AdBreakPlacement(AdBreak.createAdBreak( 
                                ads,       // Vector<Ad> 
                                time,      // Ad Break start time. Note: local time on the timeline 
                                duration,  // Ad Break duration 
                                tag()      // An arbitrary string value that can be attached to  
                                           // the AdBreak object. 
                               ), placementInformation  // Retrieved from PlacementOpportunity 
      )
      
    3. 解決廣告後,請呼叫下列其中一個函式:

      • 如果廣告解析成功:notifyResolveComplete(Vector<TimelineOperation> proposals)
      • 如果廣告解析失敗:notifyResolveError(Error error)

      例如,如果失敗:

      Metadata metadata = new MetadataNode(); 
      metadata.setValue("NATIVE_ERROR_CODE", exception.getCause().toString()); 
      error.setMetadata(metadata);
      

此範例自訂廣告解析程式會向廣告伺服器發出HTTP要求,並接收JSON回應。

public class CustomAdResolver extends ContentResolver { 
    ... 
    @Override 
    protected void doResolveAds(Metadata metadata, PlacementOpportunity placementOpportunity) { 
        ... 
        if (resolveSuccess == true) { 
            notifyResolveComplete(Vector<TimelineOperation> proposals); 
        } 
        else { 
            notifyResolveError(Error error); 
        } 
    } 
    ... 
}

即時串流的範例JSON廣告伺服器回應:

{     
    "response": { 
        "breaks": [ { 
            "start": 0, 
            "ads": [ { 
                "id": 1001, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/geico/playlist.m3u8", 
                    "duration": 30000, 
                    "id": "asset1", 
                    "resource_type": "" 
                }, 
                "companion_assets": { 
                } 
            } 
        ] }, 
        { 
            "start": -1, 
            "ads": [ { 
                "id": 1003, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/priceline/playlist.m3u8", 
                    "duration": 30000, 
                    "id": "asset3", 
                    "resource_type": "" 
                }, 
                "companion_assets": { 
                } 
            } ] 
        } ] 
    } 
} 

VOD的範例JSON廣告伺服器回應:

{     
    "response": { 
        "breaks": [ { 
            "start": 0, 
            "ads": [ { 
                "id": 1001, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/geico/playlist.m3u8", 
                    "duration": 30000, 
                    "id": "asset1", 
                    "resource_type": "" 
                }, 
                "companion_assets": {  
                } 
            }, 
            { 
                "id": 1002, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/crescent/playlist.m3u8", 
                    "duration": 15000, 
                    "id": "asset2", 
                    "resource_type": "" 
                }, 
                "companion_assets": { 
                } 
            } ] 
        }, 
        { 
            "start": 50000, 
            "ads": [ { 
                "id": 1003, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/priceline/playlist.m3u8", 
                    "duration": 30000, 
                    "id": "asset3", 
                    "resource_type": "" 
                }, 
                "companion_assets": { 
                } 
            } ] 
        }, 
        { 
            "start": 100000000, 
            "ads": [ { 
                "id": 1004, 
                "primary_asset": { 
                    "url": "https://venkat-test.s3.amazonaws.com/ads/camry/playlist.m3u8", 
                    "duration": 15000, 
                    "id": "asset4", 
                    "resource_type": "" 
                }, 
                "companion_assets": { 
                } 
            } ] 
        } ] 
    } 
} 

本頁內容