在设备之间传输
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
当用户使用无线技术连接电视、家庭影院系统和音乐播放器时,他们是希望能够在这些拥有更大屏幕、更高音量的设备上播放来自 Android 应用的内容。实现这种形式的播放,可以将单个设备上的单用户应用转变成一种可以多个用户共享的美好体验。
Android MediaRouter API 旨在利用通用界面在远程接收端设备上实现媒体内容的显示和播放。然后,实现 MediaRouter
接口的应用开发者可以连接到框架并将内容播放到加入 MediaRouter 框架的设备上。媒体播放设备制造商可以通过以下方式加入到框架中:发布 MediaRouteProvider
以允许其他应用连接到接收端设备并在上面播放媒体。图 1 说明了应用如何通过 MediaRouter 框架连接到接收端设备。
图 1. 有关媒体路由提供程序类如何提供从媒体应用到接收端设备的通信的概览。
注意:如果您想让您的应用支持 Google Cast 设备,则应使用 Cast SDK 并将您的应用构建为为 Cast 发送端。请不要直接使用 MediaRouter 框架,而应遵循 Cast 文档中的说明操作。
MediaRouter 支持库
MediaRouter API 是在 AndroidX MediaRouter 库中定义的。该支持库与搭载 Android 2.3(API 级别 9)及更高版本的设备兼容,可确保在所有这些设备上提供一致的体验。
如需详细了解 MediaRouter API,请参阅 API 参考文档中的 androidx.mediarouter.media
软件包。
- MediaRouter API
- 媒体应用使用
MediaRouter
API 来发现可用的远程播放设备并将音频和视频传送给这些设备。
- MediaRouteProvider API
MediaRouteProvider
API 用于定义远程播放设备的功能,并使其对使用 MediaRouter
搜索备用媒体路径的应用可见。
输出切换器

从 Android 11 开始,应用的路由选项也会显示在系统媒体播放器中。当用户改变视听环境时(例如在厨房中观看视频与在手机上观看之间切换,或者在家中收听音频与在车中收听之间切换),这有助于为用户提供无缝切换设备的流畅体验。
默认情况下,按媒体通知中的路由选择按钮后,系统会打开输出切换器,其中包含以下选项:
应用还可以根据其功能提供更多选项,例如“投射”。
应用可以使用 MediaRouter
API 自定义路由选择。您可以排除不支持的设备(例如,如果您在观看 Netflix 智能电视,就可以滤除纯音频的 Chromecast),也可以添加应用可识别的其他特殊设备。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Routing between devices\n\nAs users connect their televisions, home theater systems, and music players with wireless\ntechnologies, they want to be able to play content from Android apps on these larger,\nlouder devices. Enabling this kind of playback can turn your one-device, one-user app\ninto a shared experience that delights and inspires multiple users.\n\n\u003cbr /\u003e\n\nThe Android media router APIs are designed to enable media display and playback on\nremote receiver devices using a common user interface. App developers that\nimplement a [MediaRouter](/reference/androidx/mediarouter/media/MediaRouter) interface can then connect to the\nframework and play content to devices that participate in the media router framework. Media\nplayback device manufacturers can participate in the framework by publishing a [MediaRouteProvider](/reference/androidx/mediarouter/media/MediaRouteProvider) that allows other applications to connect to and\nplay media on the receiver devices. Figure 1 illustrates how an app connects to a receiver\ndevice through the media router framework.\n\n\u003cbr /\u003e\n\n\n**Figure 1.** Overview of how media route provider classes provide communication\nfrom a media app to a receiver device.\n\n\u003cbr /\u003e\n\n\n**Note:** If you want your app to support\n[Google Cast](https://developers.google.com/cast/) devices,\nyou should use the [Cast SDK](https://developers.google.com/cast/docs/reference/)\nand build your app as a Cast sender. Follow the directions in the\n[Cast documentation](https://developers.google.com/cast/docs/android_sender_setup)\ninstead of using the MediaRouter framework directly.\n\nMediaRouter support library\n---------------------------\n\nThe mediarouter APIs are defined in the\n[AndroidX MediaRouter library](/guide/topics/media/mediarouter).\nThis library is compatible with devices running Android 2.3 (API level 9) and higher and ensures a consistent\nexperience across all of them.\nFor detailed information about the mediarouter APIs, see the [androidx.mediarouter.media](/reference/androidx/mediarouter/media/package-summary)\npackage in the API reference.\n\n**[MediaRouter API](/guide/topics/media/mediarouter)**\n: A media app uses the `MediaRouter` API to discover available remote playback devices and to route audio and video to them.\n\n**[MediaRouteProvider API](/guide/topics/media/mediarouteprovider)**\n: The `MediaRouteProvider` API defines the capabilities of a remote playback device and makes it visible to apps that use a `MediaRouter` to search for alternative media paths.\n\nThe output switcher\n-------------------\n\nStarting with Android 11, your app's routing\noptions also appear in the system media player. This helps to give the user a\nseamless journey when moving between devices as they change their viewing and\nlistening contexts, such as watching video in the kitchen versus on a phone,\nor listening to audio in the home or car.\n\nPressing the route selection button in a media notification brings up the\noutput switcher with these choices by default:\n\n- The speaker on the current device\n- All connected Bluetooth audio devices\n\nApps can also provide more options depending on their capabilities, such as Cast.\n\nApps can use the [`MediaRouter`](/reference/androidx/mediarouter/media/MediaRouter)\nAPI to customize\nthe routing choices. You can exclude devices you don't support (like filtering\nout audio-only Chromecast if you're watching a Netflix smart TV) or include\nother special devices that your app recognizes."]]