将媒体应用扩展到 Android TV
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 提供了针对在大屏设备(如高清电视)上运行的应用进行优化的丰富用户体验。您可以为 Android TV 开发应用,从而扩大应用受众群体。本文档提供了有关如何有效执行此操作的指导。
构建 TV 应用
TV 应用使用的结构与手机和平板电脑的应用相同。这表示您可以根据自身对 Android 应用的既有知识创建新的 TV 应用,也可以扩展现有应用,使其支持 TV 设备。
不过,TV 与手机和平板电脑设备在用户交互模式上差异很大。为了让您的应用在电视设备上取得成功,您必须设计新的布局,使用户能在距离电视 10 英尺的地方轻松看清楚屏幕内容,并且只需使用方向键和选择按钮便可完成导航。
如需详细了解如何处理 TV 硬件控制器、构建 TV 布局和创建 TV 导航等注意事项,请参阅构建 TV 应用。
Jetpack Media3 提供了一个播放器接口,用于定义基本功能,例如播放、暂停、跳转和显示轨道信息。ExoPlayer 是 Media3 中此接口的默认实现。
与 Android 的 MediaPlayer 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 中,默认播放器是实现 Player 接口的 ExoPlayer 类。将媒体会话连接到播放器后,应用便可对外宣传媒体播放,并接收来自外部来源的播放命令。媒体会话会将这些命令委托给媒体应用的播放器。
媒体会话是控制播放的关键。它可让您将来自外部来源的命令路由到负责播放媒体的播放器。外部客户端可以使用媒体控制器向您的媒体应用发出播放命令。这些命令由您的媒体会话接收,最终会委托给媒体播放器。
如需详细了解播放,例如如何自定义播放命令行为,请参阅使用 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 文档。
您还可以帮助用户:
通过提供媒体操作 Feed 或集成“接下来观看”功能,在多个平台中发现内容。
通过支持账号关联和授权同步、提供语音投屏功能以及启用 Cast Connect,充分利用语音和互动功能。
通过集成 Google Play 结算服务并提供顺畅的订阅体验,更轻松地付款。
观看直播电视节目以及其他基于频道的连续内容是电视体验的重要组成部分。用户习惯于通过频道浏览选择和观看电视节目。TV 输入框架会创建频道以在 TV 收视指南中发布视频或音乐内容。
TV 输入框架提供了一种统一的方法,既可以接收和播放硬件来源(例如 HDMI 端口和内置调谐器)中的直播视频内容,也可以接收和播放软件来源(例如通过互联网在线播放的内容)中的直播视频内容。如需了解详情,请参阅构建电视输入源服务。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-03。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-09-03。"],[],[],null,["Android 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\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\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\nUse 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\nKotlin \n\n```kotlin\nval player = ExoPlayer.Builder(context).build()\nval mediaSession = MediaSession.Builder(context, player).build()\n```\n\nJava \n\n```java\nExoPlayer player = new ExoPlayer.Builder(context).build();\nMediaSession mediaSession = new MediaSession.Builder(context, player).build();\n```\n\nAutomatic 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\nControl 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\nAvoid 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\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\nTV 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\nBest 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\nBuild 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)."]]