Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Il framework Cast fornisce classi di accodamento che supportano la creazione di elenchi
di istanze MediaQueueItem, che possono essere create da istanze MediaInfo come stream video o audio, da riprodurre in sequenza sul ricevitore. Questa coda di elementi di contenuti
può essere modificata, riordinata, aggiornata e così via.
L'SDK Receiver gestisce la coda e risponde alle operazioni sulla coda finché la coda ha almeno un elemento attualmente attivo (in riproduzione o in pausa). I mittenti possono partecipare alla sessione e aggiungere elementi alla coda. Il destinatario
mantiene una sessione per gli elementi della coda finché l'ultimo elemento non viene riprodotto o
il mittente non interrompe la riproduzione e termina la sessione oppure finché un mittente
non carica una nuova coda sul destinatario. Il destinatario non gestisce
alcuna informazione sulle code terminate per impostazione predefinita. Al termine dell'ultimo elemento della coda, la sessione multimediale termina e la coda scompare.
Creare e caricare elementi della coda multimediale
Un elemento della coda multimediale è rappresentato nel framework Cast come un'istanza di
MediaQueueItem.
Quando crei un elemento della coda multimediale, se utilizzi la libreria
del lettore multimediale con contenuti adattivi, puoi impostare
il tempo di precaricamento in modo che il player possa iniziare a memorizzare nella cache l'elemento della coda multimediale prima
che l'elemento precedente nella coda termini la riproduzione. Se imposti l'attributo di riproduzione automatica
dell'elemento su true, il destinatario può riprodurlo automaticamente. Ad esempio,
puoi utilizzare un pattern builder per creare l'elemento della coda multimediale nel seguente modo:
Carica un array di elementi della coda multimediale nella coda utilizzando il metodo
queueLoad
appropriato di RemoteMediaClient.
Ricevere aggiornamenti sullo stato della coda dei contenuti multimediali
Quando il destinatario carica un elemento della coda multimediale, gli assegna un ID univoco
che persiste per la durata della sessione (e della coda). La tua
app può conoscere lo stato della coda in termini di elemento attualmente caricato
(potrebbe non essere in riproduzione), in caricamento o precaricato. La classe
MediaStatus
fornisce queste informazioni sullo stato:
getPreloadedItemId()
method - If the next item has been preloaded, returns the preloaded item ID.
getLoadingItemId()
method - Returns the item ID of the item that is currently loading (but isn't
active in the queue) on the receiver.
getCurrentItemId()
method - Returns the item ID of the item that that was active in the queue (it
might not be playing) at the time the media status change happened.
Metodo getQueueItems()
(deprecato, utilizza MediaQueue) - Restituisce l'elenco delle istanze
MediaQueueItem come elenco non modificabile.
La tua app può anche ottenere l'elenco degli elementi utilizzando la classe
MediaQueue. La classe è un modello di dati sparsi della coda multimediale. Mantiene l'elenco degli ID
degli elementi nella coda, che viene sincronizzato automaticamente con il ricevitore.
MediaQueue non conserva tutti i
MediaQueueItem
perché occuperebbe troppa memoria quando la coda è molto lunga. Recupera invece gli elementi su richiesta e conserva un LruCache degli elementi a cui è stato eseguito l'accesso di recente. Puoi utilizzare questi metodi per accedere alla coda dei contenuti multimediali:
Metodo getItemIds(): restituisce l'elenco di tutti gli ID elemento in ordine.
Metodo getItemAtIndex(): restituisce l'elemento memorizzato nella cache in un determinato indice. Se l'elemento non è memorizzato nella cache,
MediaQueue restituirà null e pianificherà il recupero dell'elemento. Quando l'elemento
viene recuperato,
MediaQueue.Callback#itemsUpdatedAtIndexes()
verrà chiamato e la chiamata di getItemAtIndex() con lo stesso ID restituirà l'elemento.
fetchMoteItemsRelativeToIndex() viene utilizzato quando l'utente scorre l'interfaccia utente della coda verso l'alto o verso il basso
e la tua app vuole recuperare altri elementi dal cloud.
Utilizza questi metodi insieme agli altri metodi di stato dei contenuti multimediali per informare la tua
app sullo stato della coda e degli elementi in coda. Oltre agli aggiornamenti dello stato dei contenuti multimediali dal destinatario, la tua app può rilevare le modifiche alla coda implementando RemoteMediaClient.Callback e MediaQueue.Callback.
Inoltre, Cast SDK fornisce due classi di utilità per creare l'interfaccia utente per la messa in coda.
Ad esempio, per creare un RecyclerView utilizzando MediaQueueRecyclerViewAdapter:
Kotlin
classMyRecyclerViewAdapter(mediaQueue:MediaQueue?):MediaQueueRecyclerViewAdapter<MyViewHolder?>(mediaQueue){overridefunonBindViewHolder(holder:MyViewHolder,position:Int){valitem=getItem(position)// Update the view using `item`....}}classMyViewHolder:RecyclerView.ViewHolder{// Implement your own ViewHolder....}funsomeMethod(){valadapter=MyRecyclerViewAdapter(mCastSession.remoteMediaClient.getMediaQueue())valrecyclerView=activity.findViewById(R.id.my_recycler_view_id)asRecyclerViewrecyclerView.adapter=adapter}
Java
publicclassMyRecyclerViewAdapterextendsMediaQueueRecyclerViewAdapter<MyViewHolder>{publicMyRecyclerViewAdapter(MediaQueuemediaQueue){super(mediaQueue);}@OverridepublicvoidonBindViewHolder(MyViewHolderholder,intposition){MediaQueueItemitem=getItem(position);// Update the view using `item`....}}publicclassMyViewHolderimplementsRecyclerView.ViewHolder{// Implement your own ViewHolder....}publicvoidsomeMethod(){RecyclerView.Adapteradapter=newMyRecyclerViewAdapter(mCastSession.getRemoteMediaClient().getMediaQueue());RecyclerViewrecyclerView=(RecyclerView)getActivity().findViewById(R.id.my_recycler_view_id);recyclerView.setAdapter(adapter);}
Modificare la coda
Per operare sugli elementi della coda, utilizza i metodi della coda della classe
RemoteMediaClient. Questi metodi consentono di caricare un array di elementi in una nuova coda, inserire elementi in una coda esistente, aggiornare le proprietà degli elementi nella coda, far avanzare o arretrare un elemento nella coda, impostare le proprietà della coda stessa (ad esempio, modificare l'algoritmo repeatMode che seleziona l'elemento successivo), rimuovere elementi dalla coda e riordinare gli elementi nella coda.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-05 UTC."],[[["\u003cp\u003eDesign a settings experience for your car screen app, focusing on essential settings and error handling flows.\u003c/p\u003e\n"],["\u003cp\u003eUtilize Android for Cars App Library templates, such as the List and Message templates, to build a user-friendly settings interface.\u003c/p\u003e\n"],["\u003cp\u003ePrioritize simplicity and ease of navigation, including only necessary settings and organizing them on a single screen if possible.\u003c/p\u003e\n"],["\u003cp\u003eAdhere to the UX requirements for the Android for Cars App Library to ensure a seamless user experience.\u003c/p\u003e\n"],["\u003cp\u003eRemember to declare a Settings activity in your app's manifest file for proper implementation.\u003c/p\u003e\n"]]],[],null,["# Queueing\n\nThe Cast framework provides queueing classes that support the creation of lists\nof [`MediaQueueItem`](/cast/docs/reference/android/com/google/android/gms/cast/MediaQueueItem)\ninstances, which can be built from `MediaInfo` instances such as video or audio\nstreams, to play sequentially on the receiver. This queue of content items\ncan be edited, reordered, updated, and so forth.\n| **Note:** Review the [Google Cast Autoplay](/cast/docs/design_checklist/cast-autoplay) for best practices when designing an autoplay/queueing experience for Cast.\n\nThe Receiver SDK maintains the queue and responds to operations on the\nqueue as long as the queue has at least one item currently active (playing or\npaused). Senders can join the session and add items to the queue. The receiver\nmaintains a session for queue items until the last item completes playback or\nthe sender stops the playback and terminates the session, or until a sender\nloads a new queue on the receiver. The receiver does not maintain any\ninformation about terminated queues by default. Once the last item in the queue\nfinishes, the media session ends and the queue vanishes.\n| **Note:** The [`Styled Media Receiver`](/cast/docs/styled_receiver) and [`Default Media Receiver`](/cast/docs/receiver_apps#default) do not support a queue of images; only a queue of audio or video streams is supported in the styled and default receivers.\n\nCreate and load media queue items\n---------------------------------\n\nA media queue item is represented in the Cast framework as a\n[`MediaQueueItem`](/android/reference/com/google/android/gms/cast/MediaQueueItem)\ninstance.\nWhen you create a media queue item, if you are using the [Media Player\nLibrary](/cast/docs/reference/player) with adaptive content, you can set the\npreload time so that the player can begin buffering the media queue item before\nthe item ahead of it in the queue finishes playing. Setting the item's autoplay\nattribute to true allows the receiver to play it automatically. For example,\nyou can use a builder pattern to create your media queue item as follows:\nKotlin \n\n```kotlin\nval queueItem: MediaQueueItem = MediaQueueItem.Builder(mediaInfo)\n .setAutoplay(true)\n .setPreloadTime(20.0)\n .build()\n```\nJava \n\n```java\nMediaQueueItem queueItem = new MediaQueueItem.Builder(mediaInfo)\n .setAutoplay(true)\n .setPreloadTime(20)\n .build();\n```\n\nLoad an array of media queue items in the queue by using the appropriate\n[`queueLoad`](/cast/docs/reference/android/com/google/android/gms/cast/framework/media/RemoteMediaClient#queueLoad(com.google.android.gms.cast.MediaQueueItem%5B%5D,%20int,%20int,%20long,%20org.json.JSONObject))\nmethod of `RemoteMediaClient`.\n\nReceive media queue status updates\n----------------------------------\n\nWhen the receiver loads a media queue item, it assigns a unique ID to the item\nwhich persists for the duration of the session (and the life of the queue). Your\napp can learn the status of the queue in terms of which item is currently loaded\n(it might not be playing), loading, or preloaded. The\n[`MediaStatus`](/android/reference/com/google/android/gms/cast/MediaStatus#constant-summary)\nclass provides this status information:\n\n- [`getPreloadedItemId()`](/android/reference/com/google/android/gms/cast/MediaStatus#getPreloadedItemId()) method - If the next item has been preloaded, returns the preloaded item ID.\n- [`getLoadingItemId()`](/android/reference/com/google/android/gms/cast/MediaStatus#getLoadingItemId()) method - Returns the item ID of the item that is currently loading (but isn't active in the queue) on the receiver.\n- [`getCurrentItemId()`](/android/reference/com/google/android/gms/cast/MediaStatus#getCurrentItemId()) method - Returns the item ID of the item that that was active in the queue (it might not be playing) at the time the media status change happened.\n- [`getQueueItems()`](/android/reference/com/google/android/gms/cast/MediaStatus#getQueueItems()) (**Deprecated, use `MediaQueue` instead** ) method - Returns the list of `MediaQueueItem` instances as an unmodifiable list.\n\nYour app can also get the list of items using the\n[`MediaQueue`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue)\nclass. The class is a sparse data model of the media queue. It keeps the list of\nitem IDs in the queue, which is automatically synchronized with the receiver.\n`MediaQueue` doesn't keep all the\n[`MediaQueueItem`](/android/reference/com/google/android/gms/cast/MediaQueueItem)\nbecause it will take too much memory when the queue is very long. Instead, it\nfetches the items on demand and keeps an [`LruCache`](https://developer.android.com/reference/android/util/LruCache.html) of\nrecently accessed items. You can use these methods to access the media queue:\n\n- [`getItemIds()`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue#getItemIds()) method - Returns the list of all item IDs in order.\n- [`getItemAtIndex()`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue#getItemAtIndex(int)) method - Returns the cached item at a given index. If the item is not cached, `MediaQueue` will return `null` and schedule to fetch the item. When the item is fetched, [`MediaQueue.Callback#itemsUpdatedAtIndexes()`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue.Callback#itemsUpdatedAtIndexes(int%5B%5D)) will be called, and calling `getItemAtIndex()` with the same ID again will return the item.\n- [`fetchMoteItemsRelativeToIndex()`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue#getItemIds(int,%0Aint,%20int)) is used when the user scrolls the queue UI to the top or bottom, and your app wants to fetch more items from the cloud.\n\nUse these methods together with the other media status methods to inform your\napp about the status of the queue and the items in the queue. In addition to\nmedia status updates from the receiver, your app can listen for changes to the\nqueue by implementing\n[`RemoteMediaClient.Callback`](/cast/docs/reference/android/com/google/android/gms/cast/framework/media/RemoteMediaClient.Callback)\nand\n[`MediaQueue.Callback`](/android/reference/com/google/android/gms/cast/framework/media/MediaQueue.Callback).\n| **Note:** To provide the best user experience, the sender app must show the next autoplay item in the queue in the sender UI.\n\nAlso, the Cast SDK provides two utility classes to create UI for queueing.\n\n- [`MediaQueueRecyclerViewAdapter`](/cast/docs/reference/android/com/google/android/gms/cast/framework/media/MediaQueueRecyclerViewAdapter), for backing the data of [`RecyclerView`](https://developer.android.com/jetpack/androidx/releases/recyclerview)\n- [`MediaQueueListAdapter`](/cast/docs/reference/android/com/google/android/gms/cast/framework/media/MediaQueueListAdapter), for backing the data of [`ListAdapter`](https://developer.android.com/reference/android/widget/ListAdapter.html).\n\nFor example, to create a `RecyclerView` using `MediaQueueRecyclerViewAdapter`:\nKotlin \n\n```kotlin\nclass MyRecyclerViewAdapter(mediaQueue: MediaQueue?) :\n MediaQueueRecyclerViewAdapter\u003cMyViewHolder?\u003e(mediaQueue) {\n override fun onBindViewHolder(holder: MyViewHolder, position: Int) {\n val item = getItem(position)\n\n // Update the view using `item`.\n ...\n }\n}\n\nclass MyViewHolder : RecyclerView.ViewHolder {\n // Implement your own ViewHolder.\n ...\n}\n\nfun someMethod() {\n val adapter = MyRecyclerViewAdapter(\n mCastSession.remoteMediaClient.getMediaQueue())\n val recyclerView =\n activity.findViewById(R.id.my_recycler_view_id) as RecyclerView\n recyclerView.adapter = adapter\n}\n```\nJava \n\n```java\npublic class MyRecyclerViewAdapter extends MediaQueueRecyclerViewAdapter\u003cMyViewHolder\u003e {\n public MyRecyclerViewAdapter(MediaQueue mediaQueue) {\n super(mediaQueue);\n }\n\n @Override\n public void onBindViewHolder(MyViewHolder holder, int position) {\n MediaQueueItem item = getItem(position);\n\n // Update the view using `item`.\n ...\n }\n}\n\npublic class MyViewHolder implements RecyclerView.ViewHolder {\n // Implement your own ViewHolder.\n ...\n}\n\npublic void someMethod() {\n RecyclerView.Adapter adapter = new MyRecyclerViewAdapter(\n mCastSession.getRemoteMediaClient().getMediaQueue());\n RecyclerView recyclerView =\n (RecyclerView) getActivity().findViewById(R.id.my_recycler_view_id);\n recyclerView.setAdapter(adapter);\n}\n```\n\nEdit the queue\n--------------\n\nTo operate on the items in the queue, use the queue methods of the\n[`RemoteMediaClient`](/cast/docs/reference/android/com/google/android/gms/cast/framework/media/RemoteMediaClient#pubmethods)\nclass. These let you load an array of items into a new queue, insert items into\nan existing queue, update the properties of items in the queue, make an item\njump forward or backward in the queue, set the properties of the queue itself\n(for example, change the `repeatMode` algorithm that selects the next item),\nremove items from the queue, and reorder the items in the queue."]]