با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
چارچوب Cast کلاسهای صفی را ارائه میکند که از ایجاد فهرستهایی از نمونههای MediaQueueItem پشتیبانی میکنند، که میتوانند از نمونههای MediaInfo مانند جریانهای ویدیویی یا صوتی ساخته شوند تا بهطور متوالی روی گیرنده پخش شوند. این صف از آیتم های محتوا را می توان ویرایش، مرتب سازی مجدد، به روز رسانی و غیره انجام داد.
Receiver SDK تا زمانی که صف دارای حداقل یک مورد فعال (در حال پخش یا توقف) باشد، صف را حفظ می کند و به عملیات روی صف پاسخ می دهد. فرستنده ها می توانند به جلسه بپیوندند و موارد را به صف اضافه کنند. گیرنده یک جلسه را برای آیتم های صف نگه می دارد تا زمانی که آخرین آیتم پخش را کامل کند یا فرستنده پخش را متوقف کند و جلسه را خاتمه دهد، یا تا زمانی که فرستنده یک صف جدید را روی گیرنده بارگذاری کند. گیرنده به طور پیش فرض هیچ اطلاعاتی در مورد صف های پایان یافته نگهداری نمی کند. پس از اتمام آخرین مورد در صف، جلسه رسانه به پایان می رسد و صف ناپدید می شود.
موارد صف رسانه را ایجاد و بارگیری کنید
یک آیتم صف رسانه در چارچوب Cast به عنوان یک نمونه MediaQueueItem نشان داده می شود. وقتی یک آیتم صف رسانه ایجاد میکنید، اگر از کتابخانه Media Player با محتوای تطبیقی استفاده میکنید، میتوانید زمان پیشبارگذاری را طوری تنظیم کنید که پخشکننده بتواند قبل از اینکه آیتم جلوتر از آن در صف پخش شود، شروع به بافر کردن آیتم صف رسانهای کند. تنظیم ویژگی پخش خودکار مورد روی true به گیرنده اجازه می دهد تا آن را به طور خودکار پخش کند. به عنوان مثال، می توانید از یک الگوی سازنده برای ایجاد آیتم صف رسانه خود به صورت زیر استفاده کنید:
با استفاده از روش queueLoad مناسب RemoteMediaClient ، آرایه ای از آیتم های صف رسانه را در صف بارگیری کنید.
به روز رسانی وضعیت صف رسانه را دریافت کنید
هنگامی که گیرنده یک آیتم صف رسانه را بارگیری می کند، یک شناسه منحصر به فرد به مورد اختصاص می دهد که در طول جلسه (و عمر صف) باقی می ماند. برنامه شما می تواند وضعیت صف را از نظر اینکه موردی که در حال حاضر بارگیری شده است (ممکن است پخش نشود)، بارگیری یا از قبل بارگیری شده است، بیاموزد. کلاس MediaStatus این اطلاعات وضعیت را ارائه می دهد:
متد getPreloadedItemId() - اگر مورد بعدی از قبل بارگذاری شده باشد، شناسه مورد از پیش بارگذاری شده را برمی گرداند.
متد getLoadingItemId() - شناسه مورد موردی را که در حال بارگیری (اما در صف فعال نیست) روی گیرنده برمیگرداند.
متد getCurrentItemId() - شناسه مورد موردی را که در زمان تغییر وضعیت رسانه در صف فعال بود (ممکن است پخش نشود) را برمیگرداند.
متد getQueueItems() ( منسوخ شده، به جای آن از MediaQueue استفاده کنید ) - لیست نمونه های MediaQueueItem را به عنوان یک لیست غیرقابل تغییر برمی گرداند.
برنامه شما همچنین میتواند فهرست موارد را با استفاده از کلاس MediaQueue دریافت کند. کلاس یک مدل داده پراکنده از صف رسانه است. لیست شناسه های اقلام را در صف نگه می دارد که به طور خودکار با گیرنده همگام می شود. MediaQueue تمام MediaQueueItem را نگه نمیدارد، زیرا زمانی که صف بسیار طولانی باشد، حافظه زیادی میگیرد. در عوض، اقلام را بر حسب تقاضا واکشی می کند و یک LruCache از مواردی که اخیراً به آنها دسترسی داشته اید را نگه می دارد. برای دسترسی به صف رسانه می توانید از این روش ها استفاده کنید:
متد getItemIds() - لیست تمام شناسه های آیتم ها را به ترتیب برمی گرداند.
متد getItemAtIndex() - آیتم کش شده را در یک شاخص معین برمی گرداند. اگر آیتم کش نباشد، MediaQueuenull برمیگرداند و برای واکشی مورد برنامهریزی میکند. هنگامی که مورد واکشی شد، MediaQueue.Callback#itemsUpdatedAtIndexes() فراخوانی می شود و با فراخوانی مجدد getItemAtIndex() با همان شناسه، مورد را برمی گرداند.
fetchMoteItemsRelativeToIndex() زمانی استفاده می شود که کاربر رابط کاربری صف را به بالا یا پایین پیمایش کند و برنامه شما بخواهد موارد بیشتری را از ابر واکشی کند.
از این روش ها همراه با سایر روش های وضعیت رسانه استفاده کنید تا برنامه خود را در مورد وضعیت صف و موارد موجود در صف مطلع کنید. علاوه بر بهروزرسانیهای وضعیت رسانه از گیرنده، برنامه شما میتواند با اجرای RemoteMediaClient.Callback و MediaQueue.Callback به تغییرات در صف گوش دهد.
همچنین، Cast SDK دو کلاس کاربردی برای ایجاد UI برای صفبندی فراهم میکند.
به عنوان مثال، برای ایجاد یک RecyclerView با استفاده از MediaQueueRecyclerViewAdapter :
کاتلین
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}
جاوا
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);}
صف را ویرایش کنید
برای کار بر روی آیتم های موجود در صف، از روش های صف کلاس RemoteMediaClient استفاده کنید. اینها به شما امکان میدهند آرایهای از آیتمها را در یک صف جدید بارگذاری کنید، آیتمها را در یک صف موجود وارد کنید، ویژگیهای آیتمها را در صف بهروزرسانی کنید، یک آیتم را به جلو یا عقب در صف بپرید، ویژگیهای خود صف را تنظیم کنید (مثلاً الگوریتم repeatMode را تغییر دهید که آیتم بعدی را انتخاب میکند)، آیتمها را از صف حذف کنید، و آیتمها را در صف مرتب کنید.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\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."]]