自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
使用 Media 實作收音機
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
收音機 UI 是以獨立應用程式的形式實作。如要瞭解如何將收音機 UI 整合至收音機硬體,請參閱「實作收音機」一文。
以下章節將說明如何將 Radio UI 與媒體整合,為使用者提供流暢的體驗,讓使用者能與媒體來源和電台互動,就像使用單一應用程式一樣。
下圖說明 Radio 和 Media 的參考實作項目如何實作應用程式切換使用者流程。

圖 1. 媒體來源切換使用者流程
為了在媒體中的電台和其他應用程式之間提供流暢的轉換效果,car-media-common
程式庫會定義可用於啟動媒體來源選取器的 Android 意圖。在 AOSP 中,這個選取器會在應用程式啟動器中實作,提供相同的 UI 來啟動應用程式,但會篩選只顯示媒體來源。
原始設備製造商 (OEM) 可以選擇採用目前的應用程式啟動器實作方式,或是導入自訂的媒體來源選取器。
選取器可在兩種模式下運作:
- 正常流程。使用選取器後,系統會在「媒體」中顯示所選來源,方便使用者瀏覽內容。
- 做為切換鈕。使用選取器切換來源,但不會向使用者顯示媒體。例如主畫面上的「選取器」圖示。選取來源後,系統會向使用者顯示最近的先前畫面 (在本例中為主畫面)。
用於切換媒體來源的意圖可從 MediaSource#getSourceSelectorIntent()
方法取得,該方法會接受 popup
布林值,該值會傳回意圖,用於啟動上述各個流程。
實際的意圖會在 packages/apps/Car/libs/car-media-common/res/values/config.xml
中定義。如要自訂這項設定,請使用建構時間疊加層。
取代「Radio」應用程式
由於 Radio 應用程式實作了媒體瀏覽和媒體工作階段,因此會在應用程式啟動器中顯示 Radio。如要避免在使用者點選圖示時啟動媒體,您必須使用兩個元素。電台必須:
- 具備啟動器活動。
- 宣告為自訂來源。如要這樣做,請將元件名稱新增至
car-media-common/res/values/config.xml
中的 custom_media_packages
鍵。
使用者體驗駕駛限制
媒體必須遵守所有使用者體驗駕駛分心限制。為此,Media 必須監聽 CarUXRestrictionManager 並實作其所有政策。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Implement radio with Media\n\nThe Radio UI is implemented as an independent app. Instructions on how to integrate a\nRadio UI to the radio hardware can be found at\n[Implement radio](/docs/automotive/broadcast-radio).\n\nThe following section describes how to integrate Radio UI with Media to provide users with a\nseamless experience that enables users to interact with media sources and radio as if they were a\nsingle app.\n\nMedia source switching user flow\n--------------------------------\n\nThe following diagram illustrates how the reference implementation of Radio and Media implements\nthe app switching user flow.\n\n**Figure 1.** Media source switching user flow\n\nTo provide a seamless transition between Radio and other apps in Media, the\n`car-media-common` library defines Android intents that can be used to launch a media\nsource selector. In AOSP, this selector is implemented in the App Launcher, presenting the same UI\nfor launching apps but filtered to display only media sources.\n\nOEMs can opt to take the current App Launcher implementation as is, or implement a customized\nmedia source selector.\n\nThe selector can act in two modes:\n\n- **Normal flow.** After using the selector, the selected source is displayed in Media so the user can browse its content.\n- **As a switch.** A selector is used to switch sources, but the media is not displayed to the user. This is true of the Selector icon on the Home Page. After selecting a source, the most recent previous screen is displayed to the user (in this case, the Home page).\n\nThe intent used to switch between media sources can be obtained from the\n`MediaSource#getSourceSelectorIntent()` method, which accepts a `popup`\nBoolean that returns an intent to launch each of the flows described above.\n\nThe actual intents are defined at\n`packages/apps/Car/libs/car-media-common/res/values/config.xml`. To\ncustomize this configuration, use build-time overlays.\n\n### Replace the Radio app\n\nGiven that the Radio app implements Media Browse and Media Session, Radio is displayed\nin the App launcher. To prevent launching Media when a user clicks the icon, two elements\nare required. Radio *must*:\n\n- Have a *launcher* activity.\n- Be declared as a *custom source* . To do so, add the component name to the `custom_media_packages` key in `car-media-common/res/values/config.xml`.\n\nUX driving restrictions\n-----------------------\n\nMedia must observe all UX driving distraction restrictions. To do so, Media must listen to the\n[CarUXRestrictionManager](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictionsManager)\nand implement all its policies.\n\n- Media must connect to the [Car](https://developer.android.com/reference/android/car/Car) library and get an instance of [CarUXRestrictionManager](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictionsManager).\n- Media must subscribe to updates in the list of [CarUxRestrictions](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions) and implement them as documented.\n- Particularly important for Media are:\n - [UX_RESTRICTIONS_NO_SETUP](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions.html#UX_RESTRICTIONS_NO_SETUP). In this case, the sign-in flow must be disabled.\n - [UX_RESTRICTIONS_LIMIT_STRING_LENGTH](https://developer.android.com/reference/android/car/drivingstate/CarUxRestrictions.html#UX_RESTRICTIONS_LIMIT_STRING_LENGTH). Error messages and other text provided by the media apps must be limited to the given length."]]