media3-cast-integration
它能做什么
这个技能指导使用 Media3 库为 Android 应用添加 Google Cast 播放功能。内容涵盖依赖、清单、Cast 选项以及用于 Compose 和 View 界面的播放器。适合希望支持 Cast 或正从旧版 Cast SDK 迁移的 Android 开发者。
适用场景
- ✓当你希望用户能将 Android 应用中的视频或音频投射到电视或音箱上的时候。
- ✓当你需要把应用从旧版 Google Cast SDK 迁移到 Media3 Cast 时。
- ✓当你要在 Compose 或 View 应用中设置 Cast 播放器和选项提供程序的时候。
技能链接
https://github.com/android/skills/blob/main/media/media3-cast-integration/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请使用 Media3 为我的 Android 视频应用添加 Google Cast,让用户可以投射到电视上。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能会检查 Media3 版本是否为 1.9.0 或更高,然后添加所需的依赖和清单条目。接着它会为应用的各个界面配置 Cast 选项和 Cast 播放器。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Prerequisites
- Jetpack Media3 version must be
>= 1.9.0. Cast isn't available in lower versions.
Glossary
CastPlayer: Media3Playerthat controls playback on both local and remote Cast devices.RemoteCastPlayer: Media3Playerthat communicates with a Cast receiver, only used for remote playback.- Google Cast SDK: Legacy casting SDK in maintenance mode, superseded by Jetpack Media3.
OptionsProvider: Interface providing configuration options to initialize GMSCastContext.
Common guidelines
- Legacy Google Cast SDK is in maintenance mode.
- For new Cast setups:
- You must use Jetpack Media3 Cast.
- You mustn't use legacy Cast SDK unless explicitly requested.
Step 1: Set up dependencies
To complete this step, you MUST ensure the following:
In the app-level build file, declare the
media3-castdependency version 1.9.0 or higher.implementation("androidx.media3:media3-cast:1.11.1")Ensure required Media3 dependencies are present:
androidx.media3:media3-exoplayerandroidx.media3:media3-sessionandroidx.media3:media3-ui-compose
If the application uses legacy Views, add
media3-ui.Enforce the same versions across all Media3 dependencies.
Use configurations in "Add build dependencies" section of Getting started with CastPlayer as the source of truth.
For apps without an existing Cast integration:
- Verify legacy Cast SDK (
libs.play.services.cast.framework) is absent.
- Verify legacy Cast SDK (
If Migrating from Legacy Cast SDK:
- Add Media3 Cast dependencies first.
- Keep existing legacy dependencies untouched at this stage to prevent compilation errors.
Step 2: Update the manifest
To complete this step, you MUST ensure the following:
- Inside the manifest's
<application>tag, declare the Cast options provider. - Use
DefaultCastOptionsProviderby default. See the "OptionsProvider" section in Getting started with CastPlayer. - Declare a custom
OptionsProvideronly if explicitly requested. See Customize CastOptions. - Ensure
INTERNETpermission is present. Don't add any unnecessary permissions. - If Migrating from Legacy Cast SDK:
- Don't delete existing custom options provider files or manifest entries.
Step 3: Implement the player and service
Architecture baseline
Before integrating Media3 Cast, an existing app follows one of two setups:
- Local-only playback: Uses Media3
ExoPlayeronly to support local playback. - Legacy Cast setup: Uses
ExoPlayerfor local playback, alongside aPlayerwrapper over the legacyRemoteMediaClientfor remote playback. The UI interfaces with aMediaSessioninteracting with aForwardingPlayer, which finally routes controls to either local or remote playback.
To complete this step, you MUST ensure the following:
- Inside the application's
MediaSessionService(orMediaLibraryService)onCreate()method, initializeExoPlayerandCastPlayer. - Use
CastPlayerby default unlessRemoteCastPlayeris explicitly requested. See the "Build a CastPlayer" section in Getting started with CastPlayer. - For
CastPlayer, pass the instance directly toMediaSession.Builder. - Replace all legacy forwarding player wrappers.
- Don't delete legacy class files yet to prevent compilation errors during migration.
Advanced: RemoteCastPlayer
Use
RemoteCastPlayeronly if explicitly requested by user.Initialize
MediaSessionwithlocalPlayerand set aSessionAvailabilityListeneronRemoteCastPlayerto transfer playback state on Cast session availability changes:class PlaybackService : MediaSessionService() { private var mediaSession: MediaSession? = null private lateinit var localPlayer: ExoPlayer private lateinit var remotePlayer: RemoteCastPlayer override fun onCreate() { super.onCreate() localPlayer = ExoPlayer.Builder(this).build() remotePlayer = RemoteCastPlayer.Builder(this).build() mediaSession = MediaSession.Builder(this, localPlayer).build() remotePlayer.setSessionAvailabilityListener( object : Sess
常见问题
它需要哪个 Media3 版本?
Media3 必须为 1.9.0 或更高版本,因为更早的版本不提供 Cast 功能。
我应该使用旧版 Cast SDK 吗?
不应该。该技能说明旧版 SDK 已处于维护模式,因此除非你要求使用旧版 SDK,新的设置应使用 Media3 Cast。
它同时适用于 Compose 和 View 吗?
适用。它涵盖在 Compose 和基于 View 的界面中的 Cast 播放。
详情
作者的原始描述
Implements Google Cast support in Android apps using Jetpack Media3. Handles adding build dependencies, updating manifest, configuring OptionsProvider, and managing CastPlayer or RemoteCastPlayer for playback in both Compose and View-based UIs. Use when adding Cast functionality or migrating from legacy Cast SDK to Media3 Cast.
repo: android/skills
热度
- GitHub 星标
- 10k
- 排名
- #11923
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/29
评论
- …
同一仓库的更多技能
agp-9-upgrade
10k帮你的智能体把 Android 应用升级到 Android Gradle Plugin 9,并处理其中的破坏性变更。不适用于多平台项目。
camerax
10k帮你的智能体使用 CameraX 构建 Android 相机功能,包括视频录制、硬件控制以及 ML Kit 和 Media3 效果。
android-for-cars-parked-apps
10k帮你的智能体把 Android 视频、游戏或浏览器应用调整为可在 Android Auto 和 Android Automotive 上以停放应用形式运行。
appfunctions
10k帮你的智能体在 Android 应用中找到关键工作流,并把它们暴露为 AI 智能体和语音命令可以运行的 AppFunctions。