Implement a custom opportunity generator

You can implement your own opportunity generators by implementing the OpportunityGenerator class.

  1. Implement your custom ContentFactory by implementing the ContentFactory interface and overriding retrieveGenerators.

    For example:

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

    For example:

    // 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. Create a custom opportunity generator class that implements the OpportunityGenerator class.

    public class CustomOpportunityGenerator implements OpportunityGenerator
    {...}
    
    1. In the custom opportunity generator, override doConfigure, doUpdate and doCleanup:

      @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(){
      }
      

      To obtain the timed metadata:

      List<TimedMetadata> tList = getItem().getTimedMetadata();
      
      
    2. For each TimedMetadata or group of TimedMetadata, create an opportunity with the following attributes:

      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. For each opportunity created, call resolve on the OpportunityGeneratorClient:getClient().resolve(opportunity);.

This is a sample custom placement opportunity detector:

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() {}
}

On this page