實施自定義業務機會生成器

通過實施OpportunityGenerator類,您可以實施自己的機會生成器。

  1. 實施ContentFactory介面並覆寫retrieveGenerators,以實作自訂ContentFactory

    例如:

    class MyContentFactory extends ContentFactory {
        @Override
        public List<OpportunityGenerator> retrieveGenerators(MediaPlayerItem item) {
            List<OpportunityGenerator> generators = new ArrayList<OpportunityGenerator>();
            generators.add(MyOpportunityGenerator(item));
            return generators;
        }
        ...
    }
    
  2. ContentFactory註冊到MediaPlayer

    例如:

    // register the custom content factory with media player
    MediaPlayerItemConfig config =  new MediaPlayerItemConfig();
    config.setAdvertisingFactory(new MyContentFactory());
    
    // this config will should be later passed while loading the resource
    mediaPlayer.replaceCurrentResource(resource, config);
    
    // OR use MediaPlayerItemLoader to pre-load a resource
    id = 23;
    itemLoader.load(resource, id, config);
    
  3. 建立實施OpportunityGenerator類的自定義業務機會生成器類。

    public class CustomOpportunityGenerator implements OpportunityGenerator
    {...}
    
    1. 在自定義業務機會生成器中,覆蓋doConfiguredoUpdatedoCleanup :

      @Override
       public void configure(MediaPlayerItem item, Context context,
       OpportunityGeneratorClient client, AdSignalingMode mode, long playhead, TimeRange playbackRange) {
      }
      
      protected void update(long playhead, TimeRange playbackRange){
      }
      
      protected void cleanup(){
      }
      

      若要取得計時中繼資料:

      List<TimedMetadata> tList = getItem().getTimedMetadata();
      
    2. 對於每個TimedMetadataTimedMetadata組,建立具有以下屬性的業務機會:

      Opportunity(
        String id,                      // Can be id from timedMetadata
        Placement placementInformation, // Placement object containing Type, time, duration
        Metadata metadataSettings,      // Ad metadata with targeting params sent to the ad provider
        Metadata customParams           // Metadata for customizing resolving and/or tracking process.
      );
      
    3. 對於建立的每個業務機會,請在OpportunityGeneratorClient:getClient().resolve(opportunity);上調用resolve

這是自訂位置機會檢測器範例:

public class MyOpportunityGenerator implements OpportunityGenerator {

     @Override
      public void configure(MediaPlayerItem item, Context context,
      OpportunityGeneratorClient client, AdSignalingMode mode, long playhead, TimeRange playbackRange) {
      }

        MediaPlayerItem item = getItem();
        MediaPlayerItemConfig itemConfig = item.getConfig();

        if (itemConfig == null || itemConfig.getAdvertisingMetadata() == null) {
            // no ad metadata, no ads
            return;
        }

        AdvertisingMetadata metadata = item.getConfig().getAdvertisingMetadata();

        AdSignalingMode mode = itemConfig.getAdSignalingMode();

        if (mode == AdSignalingMode.CUSTOM_RANGES)
        {
            // don't override custom ad ranges
            return;
        }

        Placement.Type pType = (mode == AdSignalingMode.MANIFEST_CUES) ?
                  Placement.Type.PRE_ROLL : Placement.Type.SERVER_MAP;
        Placement.Mode pMode = Placement.Mode.DEFAULT;
        Placement placement = new Placement(pType, playhead,
                  Placement.UNKNOWN_DURATION, pMode);

        Opportunity opportunity = new Opportunity("initialOpportunity", placement,
                  metadata, null);

        OpportunityGeneratorClient client = getClient();
        client.resolve(opportunity);
    }

    @Override
    protected void update(long playhead, TimeRange playbackRange) {

 ...
 timedMetadataList = getItem().getTimedMetadata();
        for (TimedMetadata timedMetadata : timedMetadataList) {
         if (isOpportunity(timedMetadata)) {   // check if given timedMetadata should
                                               // be considered as an opportunity
  // create a PlacementOpportunity object and add it to the opportunities list
                Opportunity opportunity = new Opportunity("id", placement, metadata, null);
                client.resolve(opportunity)
          }
        }
    }

    @Override
    protected void cleanup() {}
}

本頁內容