排队

Cast 中的队列模型与 MediaSession。 Cast Connect 库不支持读取由 MediaSession

处理队列加载和设置队列信息

队列加载与加载单个项的方式相同(通过开始 具有 intent 的 activity)。您需要使用我们的客户端库来解析 intent 转换为 MediaLoadRequestData。 通过 MediaQueueData 字段包含要加载的队列信息。

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 以更新队列。如果您要创建新的队列项,请确保将 调用商品 ID setItemId() 使用构建器 MediaQueueManager.autoGenerateItemId() 以生成商品 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());