Ad Breaks

The Android TV Receiver SDK features native support for ad breaks and companion ads within a given media stream.

See the Web Receiver Ad Breaks Overview for more information on how Ad Breaks work.

Load with ad breaks on sender

On the senders, you can specify ad breaks in a load command using AdBreakClipInfo and AdBreakInfo:

Android

AdBreakClipInfo breakClip1 =
    new AdBreakClipInfo.Builder("bc1")
        .setTitle("Clip title")
        .setPosterUrl("https://www.some.url")
        .setDuration(60000)
        .setWhenSkippableInMs(5000)  // Set this field so that the ad is skippable
        .build();

AdBreakClipInfo breakClip2 = …
AdBreakClipInfo breakClip3 = …

AdBreakInfo break1 =
    new AdBreakInfo.Builder(/* playbackPositionInMs= */ 10000)
        .setId("b1")
        .setBreakClipIds({"bc1","bc2"})
        …
        .build();

MediaInfo mediaInfo = new MediaInfo.Builder()
    …
    .setAdBreaks({break1})
    .setAdBreakClips({breakClip1, breakClip2, breakClip3}).build();

MediaLoadRequestData mediaLoadRequestData = new MediaInfo.Builder()
    …
    .setMediaInfo(mediaInfo).build();

remoteMediaClient.load(mediaLoadRequestData);

Handling load with ad breaks on Android TV

On your Android TV app, the breaks will be included in MediaLoadRequestData. Load requests can be processed normally and the AdBreakClips and AdBreaks can be retrieved from the MediaInfo:

public class MyMediaLoadCommandCallback extends MediaLoadCommandCallback {
  @Override
  public Task<MediaLoadRequestData> onLoad(String senderId, MediaLoadRequestData loadRequestData) {
    return Tasks.call(() -> {
        // Resolve the entity into your data structure and load media.
        MediaInfo mediaInfo = loadRequestData.getMediaInfo();

        ...

        myPrepareAdBreaks(mediaInfo.getAdBreakClips(), mediaInfo.getAdBreaks());

        // Update media metadata and state (this clears all previous status
        // overrides).
        castReceiverContext.getMediaStatusModifier()
            .setDataFromLoad(mediaInfo); // Ad breaks are set on the modifier.

        castReceiverContext.getMediaManager().broadcastMediaStatus();

        // Return the resolved MediaLoadRequestData to indicate load success.
        return loadRequestData;
    });
}

Updating ad breaks on Android TV

When ads start playing you need to update the AdBreakStatus on the MediaStatusModifier to broadcast that your app has started playing ads:

AdBreakStatus breakStatus =
    new AdBreakStatus.Builder()
        .setBreakId("b1")
        .setBreakClipId("bc1")
        .setCurrentBreakClipTimeInMs(breakClipProgress)
        .setCurrentBreakTimeInMs(breakProgress)
        .setWhenSkippableInMs(5000)  // Set this field so that the ad break clip is skippable
        .build();

castReceiverContext.getMediaStatusModifier()
    .setAdBreakStatus(breakStatus);

You can also dynamically modify the ad breaks after an item is loaded:

AdBreakClipInfo breakClip1 = ...
AdBreakClipInfo breakClip2 = ...
AdBreakClipInfo breakClip3 = ...

AdBreakInfo break1 = ...
AdBreakInfo break2 = ...

mediaManager.getMediaStatusModifier().getMediaInfoModifier()
    .setAdBreakClips({breakClip1, breakClip2, breakClip3})
    .setAdBreaks({break1, break2});

Enable and handle ad skipping

When an ad break is playing, the senders will show a button to skip the current ad break clip if it is skippable. To enable the ability for a user to skip an ad break clip, use the MediaStatusModifier to add the COMMAND_SKIP_AD media command:

mMediaManager.getMediaStatusModifier().setMediaCommandSupported(MediaStatus.COMMAND_SKIP_AD, true);

To handle the SKIP_AD command, implement the onSkipAd callback in your CastActionCallbacks:

public MyMediaCommandCallback extends MediaCommandCallback {
  @Override
  public Task<Void> onSkipAd(RequestData requestData) {
    // Skip your ad
    ...
    return Tasks.forResult(null);
  }
}

MediaManager mediaManager =
    CastReceiverContext.getInstance().getMediaManager();
mediaManager.setMediaCommandCallback(new MyMediaCommandCallback());

Client-side stitching

Client-side stitching is where ads are not embedded in the stream. For Cast Connect, in addition to updating the AdBreakStatus on the MediaStatusModifier you will need to set the playback speed to 0 on the PlaybackStateCompat so the senders know to freeze the content timeline progress.

// Playback speed should be 0 if content is not playing.
if (adIsPlaying) {
    playbackSpeed = 0.0f;
}
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
    .setActions(AVAILABLE_MEDIA_ACTIONS);
stateBuilder.setState(playbackStateCompat, position, playbackSpeed);
mediaSession.setPlaybackState(stateBuilder.build());

Once the ad finishes, you should resume the previous playback speed.

Server-side stitching

For server-side stitching, the ads are embedded so the server is expected to provide a single stream that contains both the content and ads. In this case, playback can continue progressing normally as the timeline contains the duration of the ad in addition to the content.