廣告插播時間點

Android TV 接收器 SDK 原生支援廣告插播和隨播廣告 特定媒體串流中的廣告

請參閱網路接收器廣告插播總覽

處理含有廣告插播的負載

在 Android TV 應用程式中,暫停時間會顯示於 MediaLoadRequestData。 系統會正常處理載入要求,並 AdBreakClipInfo 秒 和 AdBreakInfo 秒 最新資訊 MediaInfo

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;
    });
    }
}

正在更新廣告插播時間點

當廣告開始播放時,請更新 AdBreakStatus敬上 的 MediaStatusModifier。 廣播您的應用程式已開始播放廣告:

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 指令,請實作 onSkipAd 回呼 MediaCommandCallback

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、 更新 AdBreakStatus敬上 的 MediaStatusModifier。 您必須將播放速度設為 0 PlaybackStateCompat 讓寄件者知道要凍結內容時間軸進度。

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());

廣告播完後,建議您恢復先前的播放速度。

伺服器端拼接

適用對象 伺服器端拼接 因為伺服器應該提供單一串流 包含內容和廣告在這種情況下,播放功能可以繼續播放 時間軸會正常進行,因為時間軸包含廣告時間長度 讀者