广告插播时间点

Android TV Receiver SDK 支持在给定媒体串流中原生插播广告和随播广告。

如需详细了解广告插播的运作方式,请参阅网站接收器广告插播概览

使用广告插播时间点处理加载

在 Android TV 应用中,广告插播时间包含在 MediaLoadRequestData 中。系统可以正常处理加载请求,并且可以从 MediaInfo 检索 AdBreakClipInfoAdBreakInfo

Kotlin
class MyMediaLoadCommandCallback : MediaLoadCommandCallback() {
  override fun onLoad( senderId: String?, loadRequestData: MediaLoadRequestData
   ): Task {
    return Tasks.call {
      // Resolve the entity into your data structure and load media.
      val mediaInfo = loadRequestData.mediaInfo
      ...
      myPrepareAdBreaks(mediaInfo.adBreakClips, mediaInfo.adBreaks)
      // 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
    }
  }
}
Java
public class MyMediaLoadCommandCallback extends MediaLoadCommandCallback {
  @Override
  public Task 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;
    });
    }
}

更新广告插播时间点

当广告开始播放时,更新 MediaStatusModifier 上的 AdBreakStatus,以广播您的应用已开始播放广告:

Kotlin
val breakStatus = 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)
Java
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);

您还可以在内容加载后动态修改广告插播时间点:

Kotlin
var breakClip1: AdBreakClipInfo = ...
var breakClip2: AdBreakClipInfo = ...
var breakClip3: AdBreakClipInfo = ...

var break1: AdBreakInfo = ...
var break2: AdBreakInfo = ...

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

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

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

启用和处理广告跳过

广告插播播放时,发送方会显示一个按钮,以便用户跳过当前的广告插播片段(如果可跳过)。如需让用户能够跳过广告插播片段,请使用 MediaStatusModifier 添加 COMMAND_SKIP_AD 媒体命令:

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

如需处理 SKIP_AD 命令,请在 MediaCommandCallback 中实现 onSkipAd 回调:

Kotlin
class MyMediaCommandCallback : MediaCommandCallback() {
    override fun onSkipAd(requestData: RequestData?): Task<Void?> {
        // Skip your ad
        ...
        return Tasks.forResult<Any?>(null)
    }
}

val mediaManager = CastReceiverContext.getInstance().mediaManager
mediaManager.setMediaCommandCallback(MyMediaCommandCallback())
Java
public class MyMediaCommandCallback extends MediaCommandCallback {
  @Override
  public Task onSkipAd(RequestData requestData) {
    // Skip your ad
    ...
    return Tasks.forResult(null);
  }
}

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

客户端拼接

客户端拼接是指广告未嵌入到直播中。对于 Cast Connect,除了在 MediaStatusModifier 上更新 AdBreakStatus 之外,您还必须在 PlaybackStateCompat 上将播放速度设为 0,以便发送方知道要冻结内容时间轴进度。

Kotlin
// Playback speed should be 0 if content is not playing.
if (adIsPlaying) {
    playbackSpeed = 0.0f
}
val stateBuilder = PlaybackStateCompat.Builder()
    .setActions(AVAILABLE_MEDIA_ACTIONS)
stateBuilder.setState(playbackStateCompat, position, playbackSpeed)
mediaSession.setPlaybackState(stateBuilder.build())
Java
// 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());

广告播放完毕后,您应恢复之前的播放速度。

服务器端拼接

对于服务器端拼接,广告会嵌入,因此服务器应提供包含内容和广告的单个串流。在这种情况下,播放可以继续正常进行,因为时间轴除了内容之外还包含广告时长。