排程

Cast 中的佇列模型與 MediaSession 中的不同。Cast Connect 程式庫不支援讀取 MediaSession 提供的佇列。

處理佇列載入作業,並設定佇列資訊

排隊載入的做法與載入單一項目的方式相同 (透過意圖啟動活動)。您必須使用我們的用戶端程式庫,將意圖解析為 MediaLoadRequestDataMediaQueueData 欄位包含要載入的佇列資訊。

Kotlin
fun handleCastLoad(requestData: MediaLoadRequestData?) {
  if (requestData.getQueueData()) {
    // If MediaQueueData is specified, this is a queue load request.
    myPlayer.load(
          requestData.getQueueData().getItems().get(0))

    // Set media status.
    castReceiverContext.getMediaManager()
        .setDataFromLoad(requestData) // This clears all status overrides.
                                      // The queue information is updated with
                                      // the MediaQueueData in the request.
        ...

    mediaSession.setPlaybackState(...)
  }
}
Java
private void handleCastLoad(MediaLoadRequestData requestData) {
  if (requestData.getQueueData()) {
    // If MediaQueueData is specified, this is a queue load request.
    myPlayer.load(
        requestData.getQueueData().getItems().get(0));

    // Set media status.
    castReceiverContext.getMediaManager()
        .setDataFromLoad(requestData) // This clears all status overrides.
                                      // The queue information is updated with
                                      // the MediaQueueData in the request.
        ...;

    mediaSession.setPlaybackState(...);
  }
}

變更佇列

如果佇列有所變更 (例如來自 queueInsert()),您可以使用 MediaQueueManager 更新佇列。如果您要建立新的佇列項目,請務必在建構工具上呼叫 setItemId(),並使用 MediaQueueManager.autoGenerateItemId() 產生項目 ID,藉此設定項目 ID。透過 setDataFromLoad() 載入的待播清單項目已設定項目 ID。

Kotlin
val mediaManager: MediaManager = castReceiverContext.getMediaManager()

val mediaQueueManager = mediaManager.getMediaQueueManager()

mediaQueueManager.getQueueItems().add( /* position= */index, mediaQueueItem)

mediaManager.broadcastMediaStatus()
Java
MediaManager mediaManager = castReceiverContext.getMediaManager();

MediaQueueManager mediaQueueManager = mediaManager.getMediaQueueManager();

mediaQueueManager.getQueueItems().add(/* position= */ index, mediaQueueItem);

mediaManager.broadcastMediaStatus();

Android TV 應用程式也會處理 onQueueUpdate() 回呼,以便跳轉至佇列項目,並略過下一/上一首:

Kotlin
class MyMediaCommandCallback : MediaCommandCallback() {
    override fun onQueueUpdate(
        queueUpdateRequestData: QueueUpdateRequestData ): Task {
        ...
        var newItemId = MediaQueueItem.INVALID_ITEM_ID
        if (queueUpdateRequestData.getJump() != null) {
            newItemId = myGetRelativeItemId(queueUpdateRequestData.getJump())
        } else if (queueUpdateRequestData.getCurrentItemId() != null) {
            newItemId = queueUpdateRequestData.getCurrentItemId()
        }
        if (newItemId != MediaQueueItem.INVALID_ITEM_ID) {
            castReceiverContext.getMediaManager().getMediaQueueManager()
                .setCurrentItemId(newItemId)
            castReceiverContext.getMediaManager().broadcastMediaStatus()
        }
    }
}

val mediaManager = CastReceiverContext.getInstance().getMediaQueueManager()
mediaManager.setMediaCommandCallback(MyMediaCommandCallback())
Java
public class MyMediaCommandCallback extends MediaCommandCallback {
  @Override
  public Task onQueueUpdate(
      QueueUpdateRequestData queueUpdateRequestData) {
    ...
    int newItemId = MediaQueueItem.INVALID_ITEM_ID;
    if (queueUpdateRequestData.getJump() != null) {
      newItemId = myGetRelativeItemId(queueUpdateRequestData.getJump());
    } else if (queueUpdateRequestData.getCurrentItemId() != null) {
      newItemId = queueUpdateRequestData.getCurrentItemId();
    }

    if (newItemId != MediaQueueItem.INVALID_ITEM_ID) {
      castReceiverContext.getMediaManager().getMediaQueueManager()
          .setCurrentItemId(newItemId);
      castReceiverContext.getMediaManager().broadcastMediaStatus();
    }
}

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

您可能還想實作與佇列相關的傳輸控制回呼,例如 onSkipToNext()onSkipToPrevious()onSkipToQueueItem()

Kotlin
class MyMediaSessionCallback : MediaSessionCompat.Callback() {
  override fun onSkipToNext() {
    // Skip to next item in queue
    ...
  }

  override fun onSkipToPrevious() {
    // Skip to previous item in queue
    ...
  }

  override fun onSkipToQueueItem(pos: Long) {
    // Skip to specified queue item
    ...
  }
    ...
}

mediaSession.setCallback(MyMediaSessionCallback())
Java
public class MyMediaSessionCallback extends MediaSessionCompat.Callback {
  @Override
  public void onSkipToNext() {
    // Skip to next item in queue
    ...
  }

  @Override
  public void onSkipToPrevious() {
    // Skip to previous item in queue
    ...
  }

  @Override
  public void onSkipToQueueItem (long pos) {
    // Skip to specified queue item
    ...
  }
  ...
}

mediaSession.setCallback(new MyMediaSessionCallback());