將媒體應用程式擴充至 Android TV
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 提供豐富的使用者體驗,並針對在高畫質電視等大螢幕裝置上執行的應用程式進行最佳化。開發 Android TV 專用應用程式,即可擴大應用程式的目標對象。本文將說明如何有效執行這項操作。
打造 TV 應用程式
TV 應用程式採用的架構與手機和平板的應用程式相同。也就是說,您可以根據已知的 Android 應用程式建構方式,建立新的 TV 應用程式,也可以擴充現有應用程式,讓這些應用程式也能在 TV 裝置上執行。
不過,電視的使用者互動模式與手機和平板電腦大不相同。如要讓應用程式在電視裝置上獲得成功,您必須設計新的版面配置,讓使用者在 10 英尺外也能清楚瞭解內容,並提供僅使用方向鍵和選取按鈕即可操作的導覽功能。
如要進一步瞭解如何處理電視硬體控制器、建構電視版面配置及建立電視導覽等注意事項,請參閱「建構 TV 應用程式」。
Jetpack Media3 提供 Player 介面,可定義播放、暫停、跳轉及顯示曲目資訊等基本功能。ExoPlayer 是 Media3 中這個介面的預設實作方式。
相較於 Android 的 MediaPlayer API,ExoPlayer API 新增了多項便利功能,例如支援多種串流通訊協定、預設音訊和視訊轉譯器,以及處理媒體緩衝的元件。
你可以自訂及擴充 ExoPlayer,並透過 Play 商店應用程式更新。詳情請參閱「Media3 ExoPlayer」。
媒體工作階段提供系統與應用程式音訊或影片播放器互動的通用方式。Media3 與先前媒體 API 的主要差異之一,就是不再需要元件之間的連接器。
新的 MediaSession
類別會採用實作 Player 介面的任何類別。ExoPlayer 和 MediaController 都是實作該介面的類別。這有助於簡化元件之間的互動。
詳情請參閱「播放器介面」。
如要進一步瞭解如何建立媒體播放應用程式,請參閱「使用 ExoPlayer 建立基本媒體播放器」。
為提供最佳體驗給媒體應用程式的終端使用者,您需要實作 MediaSession
。如要這麼做,請初始化 Player
,並提供給 MediaSession.Builder
,如下所示:
Kotlin
val player = ExoPlayer.Builder(context).build()
val mediaSession = MediaSession.Builder(context, player).build()
Java
ExoPlayer player = new ExoPlayer.Builder(context).build();
MediaSession mediaSession = new MediaSession.Builder(context, player).build();
自動處理狀態
Media3 程式庫會使用播放器的狀態自動更新媒體工作階段。因此您不需要手動處理從玩家到工作階段的對應。這有助於確保使用者隨時看到播放媒體的最新資訊,包括「聽聲辨曲」資訊卡。
控制及通告播放功能
在 Media3 中,預設播放器是 ExoPlayer 類別,可實作 Player 介面。將媒體工作階段連結至播放器後,應用程式就能在外部宣傳媒體播放作業,並接收來自外部來源的播放指令。媒體工作階段會將這些指令委派給媒體應用程式的播放器。
媒體工作階段是控制播放的關鍵。您可以將外部來源的指令,傳送至負責播放媒體的播放器。外部用戶端可以使用媒體控制器,向媒體應用程式發出播放指令。這些指令會由媒體工作階段接收,最終再委派給媒體播放器。
如要進一步瞭解播放功能,例如如何自訂播放指令行為,請參閱「使用 MediaSession 控制及通告播放功能」。
避免應用程式中斷
使用 MediaSession
可避免不必要的干擾,例如:
關閉電視或切換電視輸入時,影片會繼續播放。這也會導致電視硬體耗電量增加。透過
MediaSession
,應用程式可以通知平台正在播放媒體,平台也能通知應用程式可以停止播放。
切換應用程式或關閉電視螢幕時,音樂播放作業會意外停止。使用 MediaSession
API 即可在背景服務中繼續播放。
限制使用者與內容互動,導致無法控制播放。例如,在應用程式於背景播放音樂時返回應用程式,或是支援語音指令。在應用程式中加入 MediaSession
,使用者就能使用語音指令搜尋及略過歌曲或集數。
其他考量
將媒體應用程式擴展至 Android TV 時,您需要考量無障礙問題、如何提高參與度、如何讓使用者尋找內容,以及如何建構遊戲和 TV 輸入服務。
電視無障礙功能
雖然輔助技術可以協助低視能使用者,但支援電視應用程式的內容探索歷程無障礙功能也很重要。
舉例來說,請特別注意提供導覽指引和正確標記元素,並確保電視應用程式能與 TalkBack 等無障礙功能搭配使用。這些步驟可大幅改善視障人士的使用體驗。
改善無障礙程度的第一步是瞭解相關知識。如要進一步瞭解文字縮放、鍵盤配置和語音說明,請參閱無障礙資源。
在 Google TV 上提高參與度的最佳做法
所有為 Android TV 打造的應用程式都可在 Google TV 裝置上運作。為提供最佳 Google TV 使用體驗,建議您採用下列最佳做法。
您需要使用 MediaSession
,提供與音訊或影片播放器互動的通用方式。如要進一步瞭解如何實作這項功能,請參閱「使用 Media3 MediaSession」。
應用程式必須支援 Google Cast。您可以透過這個 SDK 擴充 Android、iOS 和 Chrome 應用程式,將音訊和影片串流至 Android TV,以及 Chromecast 和 Google 助理裝置。詳情請參閱 Google Cast 說明文件。
您也可以協助使用者:
在各個平台探索內容:提供媒體動作動態消息,或整合「接著看」功能。
善用語音和參與度:支援帳戶連結和授權同步,提供語音投放功能,並啟用 Cast Connect。
整合 Google Play 結帳系統,提供更輕鬆的付款方式,以及流暢的訂閱體驗。
觀看直播電視節目和其他連續的頻道內容,是電視體驗的重要環節。使用者習慣透過瀏覽頻道,在電視上選取及觀看節目。電視輸入架構會建立頻道,在電視節目表發布影片或音樂內容。
電視輸入架構提供統一方法,可接收及播放來自硬體來源 (例如 HDMI 連接埠和內建調諧器) 的直播影片內容,以及來自軟體來源 (例如透過網際網路串流播放的影片)。詳情請參閱「建構 TV 輸入服務」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-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-08-27 (世界標準時間)。"],[],[],null,["# Extend your media app to Android TV\n\nAndroid offers a rich user experience that's optimized for apps running on large\nscreen devices, such as high-definition televisions. You can extend your app's\naudience by developing it for Android TV. This document provides guidance on how\nto do it effectively.\n\nBuild TV apps\n-------------\n\nTV apps use the same structure as those for phones and tablets. This approach\nmeans you can create new TV apps based on what you already know about building\napps for Android, or extend your existing apps to also run on TV devices.\n\nHowever, the user interaction model for TV is substantially different from phone\nand tablet devices. In order to make your app successful on TV devices, you must\ndesign new layouts that can be clearly understood from 10 feet away, and provide\nnavigation that works with just a directional pad and a select button.\n\nFor more information about considerations such as how to handle TV hardware\ncontrollers, building TV layouts, and creating TV navigation, see\n[Build TV apps](/training/tv/start).\n\nUse Media3 ExoPlayer\n--------------------\n\nJetpack Media3 provides a Player interface that defines basic features such as\nthe ability to play, pause, seek, and display track information. ExoPlayer is\nthe default implementation of this interface in Media3.\n\nCompared to Android's MediaPlayer API, it adds additional conveniences such as\nsupport for multiple streaming protocols, default audio and video renderers,\nand components that handle media buffering.\n\nYou can customize and extend ExoPlayer, and it can be updated through Play Store\napplication updates. For more information,\nsee [Media3 ExoPlayer](/guide/topics/media/exoplayer).\n\n### Use Media3 `MediaSession`\n\nMedia sessions provide a universal way for the system to interact with your\napp's audio or video player. One of the primary characteristics that\ndistinguishes Media3 from previous media APIs is that there is no longer a need\nfor connectors between components.\n\nThe new `MediaSession` class takes any class that implements the Player\ninterface. Both ExoPlayer and MediaController are classes which implement that\ninterface. This facilitates much simpler interaction between the components.\nFor more information, see [The Player interface](/guide/topics/media/session/player).\n\nFor more information about creating a media playback app, see\n[Create a basic media player using ExoPlayer](/media/implement/playback-app).\n\nTo create the best experience for the end users of your media app, you need to\nimplement `MediaSession`. To do so, initialize a `Player` and supply it to\n`MediaSession.Builder` like this: \n\n### Kotlin\n\n```kotlin\nval player = ExoPlayer.Builder(context).build()\nval mediaSession = MediaSession.Builder(context, player).build()\n```\n\n### Java\n\n```java\nExoPlayer player = new ExoPlayer.Builder(context).build();\nMediaSession mediaSession = new MediaSession.Builder(context, player).build();\n```\n\n#### Automatic state handling\n\nThe Media3 library automatically updates the media session using the player's\nstate. As such, you don't need to manually handle the mapping from player to\nsession. This helps ensure that your users always see the up-to-date information\non the playing media, including in the [Now Playing card](/training/tv/playback/now-playing).\n\n### Control and advertise playback\n\nIn Media3, the default player is the ExoPlayer class, which implements the\nPlayer interface. Connecting the media session to the player allows an app to\nadvertise media playback externally and to receive playback commands from\nexternal sources. The media session delegates these commands to the media\napp's player.\n\nThe media session is the key to controlling playback. It lets you route\ncommands from external sources to the player that does the work of playing your\nmedia. External clients can use a media controller to issue playback commands\nto your media app. These are received by your media session, which ultimately\ndelegates commands to the media player.\n| **Note:** When playing media in the background, your MediaSession and the player need to be housed within a MediaSessionService or MediaLibraryService that runs as a foreground service. For more information, see [Background playback with a MediaSessionService](/guide/topics/media/session/mediasessionservice).\n\nFor more information about playback, such as how to customize playback command\nbehavior, see [Control and advertise playback using MediaSession](/guide/topics/media/session/mediasession#creating-media).\n\n### Avoid disruptions in your app\n\nUsing `MediaSession` lets you avoid unnecessary disruptions such as:\n\n- **Unexpected and continued playback** when switching off the TV or switching\n TV inputs. This also causes high energy consumption for TV hardware. With\n `MediaSession`, your app can inform the platform that it's playing media, and\n the platform is able to inform the app that playback can stop.\n\n- **Music playback stops unexpectedly** when switching out of the app, or\n switching off the TV display. Using `MediaSession` APIs enables continued\n playback in a background service.\n\n- **Restricted interaction with content** that inhibits users from controlling\n playback. For example, returning to your app if it's playing music in the\n background, or supporting voice commands. With `MediaSession` in your app,\n users can [use voice commands](/media/implement/assistant) to seek and skip\n songs or episodes.\n\nFurther Considerations\n----------------------\n\nAs you extend your media app to Android for TV, you need to consider\naccessibility issues, how to drive engagement, how to enable users to find\ncontent, as well as how to build games and TV input services.\n\n### TV accessibility\n\nAlthough assistive technologies can and do help users with low vision, it's\nimportant to support accessibility in content discovery journeys for TV apps.\n\nFor example, pay extra attention to providing navigation guidance and properly\nlabeling elements, and help ensure that TV apps work well with accessibility\nfeatures like TalkBack. These steps can significantly improve the experience\nfor users with vision impairments.\n\nThe first step toward improving accessibility is awareness. For more information\nabout text scaling, keyboard layouts, and audio descriptions, see\n[accessibility resources](/training/tv/accessibility#android_accessibility_resources).\n\n### Best practices to drive engagement on Google TV\n\nAll apps built for Android TV work on devices running Google TV. To provide the\nbest user experience on Google TV, we recommend that you apply the following\nbest practices.\n\nYou need to use `MediaSession`, to provide a universal way of interacting with\nan audio or video player. For more information about how to implement this, see\n[Use Media3 MediaSession](/media/implement/surfaces/android-tv#use_media3_mediasession).\n\nAs a baseline, your app needs to support Google Cast. It lets you extend your\nAndroid, iOS, and Chrome apps to enable audio and video streaming to Android TVs\nas well as Chromecast devices and Assistant devices. For more information, see\nthe [Google Cast documentation](https://developers.google.com/cast/docs/overview).\n\nYou also can help users:\n\n- **Discover content across surfaces** by offering a media actions feed, or\n integrating Watch Next.\n\n- **Take advantage of voice and engagement** by supporting account linking and\n entitlement sync, offering voice casting, and enabling Cast Connect.\n\n- **Pay more easily** by integrating Google Play billing, and providing\n frictionless subscriptions.\n\n### Build TV input framework\n\nWatching live TV shows and other continuous, channel-based content is a big part\nof the TV experience. Users are accustomed to selecting and watching shows on\nTV by channel browsing. The TV Input Framework creates channels for publishing\nideo or music content in the TV programming guide.\n\nThe TV Input Framework provides a unified method for the receiving and playback\nof live video content from hardware sources, such as HDMI ports and\nbuilt-in-tuners, and software sources, such as video streamed over the internet.\nFor further information, see [Build TV input services](/training/tv/tif)."]]