Cast 中的队列模型不同于 MediaSession
中的队列模型。Cast Connect 库不支持读取 MediaSession
提供的队列。
处理队列加载和设置队列信息
队列加载的方式与加载单个项的方式相同(通过 intent 启动 activity)。您需要使用我们的客户端库将 intent 解析为 MediaLoadRequestData
。MediaQueueData
字段包含要加载的队列信息。
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(...) } }
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,然后设置该项 ID。通过 setDataFromLoad()
加载的队列中的项已设置其项 ID。
val mediaManager: MediaManager = castReceiverContext.getMediaManager() val mediaQueueManager = mediaManager.getMediaQueueManager() mediaQueueManager.getQueueItems().add( /* position= */index, mediaQueueItem) mediaManager.broadcastMediaStatus()
MediaManager mediaManager = castReceiverContext.getMediaManager(); MediaQueueManager mediaQueueManager = mediaManager.getMediaQueueManager(); mediaQueueManager.getQueueItems().add(/* position= */ index, mediaQueueItem); mediaManager.broadcastMediaStatus();
Android TV 应用还会处理 onQueueUpdate()
回调,以支持跳转到队列项和跳过下一项/上一项:
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())
public class MyMediaCommandCallback extends MediaCommandCallback { @Override public TaskonQueueUpdate( 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()
。
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())
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());