跳到正文
Skillpedia
移动应用10k · #11923

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/android/skills/blob/main/media/media3-cast-integration/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 : Media3 Player that controls playback on both local and remote Cast devices.
  • RemoteCastPlayer : Media3 Player that 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 GMS CastContext.

Common guidelines

Step 1: Set up dependencies

To complete this step, you MUST ensure the following:

  • In the app-level build file, declare the media3-cast dependency version 1.9.0 or higher.

    implementation("androidx.media3:media3-cast:1.11.1")
    
  • Ensure required Media3 dependencies are present:

    • androidx.media3:media3-exoplayer
    • androidx.media3:media3-session
    • androidx.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.
  • 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 DefaultCastOptionsProvider by default. See the "OptionsProvider" section in Getting started with CastPlayer.
  • Declare a custom OptionsProvider only if explicitly requested. See Customize CastOptions.
  • Ensure INTERNET permission 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 ExoPlayer only to support local playback.
  • Legacy Cast setup: Uses ExoPlayer for local playback, alongside a Player wrapper over the legacy RemoteMediaClient for remote playback. The UI interfaces with a MediaSession interacting with a ForwardingPlayer, which finally routes controls to either local or remote playback.

To complete this step, you MUST ensure the following:

  • Inside the application's MediaSessionService (or MediaLibraryService) onCreate() method, initialize ExoPlayer and CastPlayer.
  • Use CastPlayer by default unless RemoteCastPlayer is explicitly requested. See the "Build a CastPlayer" section in Getting started with CastPlayer.
  • For CastPlayer, pass the instance directly to MediaSession.Builder.
  • Replace all legacy forwarding player wrappers.
  • Don't delete legacy class files yet to prevent compilation errors during migration.

Advanced: RemoteCastPlayer

  • Use RemoteCastPlayer only if explicitly requested by user.

  • Initialize MediaSession with localPlayer and set a SessionAvailabilityListener on RemoteCastPlayer to 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能