加入队列

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

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

队列加载方式与加载单个项的方式相同(通过 intent 启动 activity)。您需要使用我们的客户端库将 intent 解析为 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 更新队列。如果要创建新的队列项,请务必使用 MediaQueueManager.autoGenerateItemId() 在构建器上调用 setItemId() 来生成项 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());