显示“正在播放”卡片
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
播放音频的 TV 应用可以在用户返回主屏幕后继续播放音频,或者
切换到其他应用。为此,应用必须在住宅中提供“正在播放”卡
屏幕。通过此卡片,用户可了解音频的来源并返回您的应用,
控制媒体播放。
每次
“MediaSession
”现为
目前,Android 框架会在主屏幕上显示“正在播放”卡片。卡片
包括专辑封面、名称和应用图标等媒体元数据。当用户选择该卡片时
系统就会打开应用
“闻曲知音”卡片
实现媒体会话后,请设置
会话变为活跃状态并请求音频焦点,系统会显示“正在播放”卡片。
注意:“正在播放”卡片仅针对媒体显示
与
FLAG_HANDLES_TRANSPORT_CONTROLS
标志已设置。此标志已在 API 级别 26 中弃用。不过,为实现向后兼容性,在旧款设备上可能仍需使用此标记。
在以下情况下,该卡会从启动器屏幕中移除:
setActive(false)
调用会停用媒体会话,或在其他应用启动媒体播放时停用。如果播放为
已完全停止且无活动的媒体,请停用媒体会话
。如果播放被暂停,请在延迟一段时间后停用媒体会话。
通常在 5 到 30 分钟内
更新银行卡
每当您的应用在 MediaSession
中更新播放状态时,
“正在播放”卡片会更新,以显示当前媒体的状态。要了解如何操作,请参阅
更新播放状态。
同样,您的应用也可以更新
MediaMetadata
可提供
“正在播放”卡片中会显示与当前媒体有关的信息,例如标题、副标题、
以及各种图标要了解如何操作,请参阅
更新媒体元数据。
响应用户操作
当用户选择“正在播放”卡片时,系统会打开拥有该卡片的应用
会话。如果您的应用提供
PendingIntent
至
setSessionActivity()
,
系统会启动您指定的 activity,如以下代码段所示。否则,
系统将打开默认系统 intent。您指定的 Activity 必须提供播放控件,
让用户可以暂停或停止播放
Kotlin
val pi: PendingIntent = Intent(context, MyActivity::class.java).let { intent ->
PendingIntent.getActivity(
context, 99 /*request code*/,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
}
session.setSessionActivity(pi)
Java
Intent intent = new Intent(context, MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
session.setSessionActivity(pi);
可接受的用例
只有当用户希望继续播放音频时,才应使用“正在播放”卡片
在你离开应用时在后台播放音乐视频播放或游戏声音应始终
暂停应用,除非您的应用集成并符合画中画模式的要求。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Display a Now Playing card\n\nTV apps that play audio may continue to do so after the user returns to the home screen or\nswitches to another app. To do so, the app must provide a **Now Playing** card on the home\nscreen. This card lets users understand where the audio is coming from and return to your app to\ncontrol media playback.\n\n\nWhenever an active\n[MediaSession](/reference/android/media/session/MediaSession) is\npresent, the Android framework displays a **Now Playing** card on the home screen. The card\nincludes media metadata such as album art, title, and app icon. When the user selects the card,\nthe system opens the app.\n\nNow Playing card\n----------------\n\n\nAfter you [implement a media session](/training/tv/playback/media-session), set the\nsession to active, and request audio focus, the **Now Playing** card appears.\n\n**Note:** The **Now Playing** card displays only for a media\nsession with the\n[FLAG_HANDLES_TRANSPORT_CONTROLS](/reference/android/media/session/MediaSession#FLAG_HANDLES_TRANSPORT_CONTROLS)\nflag set. This flag is deprecated on API level 26. However, this flag could still be needed on older devices for backwards compatibility.\n\n\nThe card is removed from the launcher screen when a\n[setActive(false)](/reference/android/media/session/MediaSession#setActive(boolean))\ncall deactivates the media session or when another app initiates media playback. If playback is\ncompletely stopped and there is no active media, deactivate the media session\nimmediately. If playback is paused, deactivate the media session after a delay,\nusually from 5 to 30 minutes.\n\nUpdate the card\n---------------\n\n\nWhenever your app updates the playback state in the `MediaSession`, the\n**Now Playing** card updates to show the state of the current media. To learn how to do this, see\n[Update the playback state](/training/tv/playback/media-session#state).\n\n\nSimilarly, your app can update the\n[MediaMetadata](/reference/android/media/MediaMetadata) to provide\ninformation to the **Now Playing** card about the current media, such as the title, subtitle,\nand various icons. To learn how to do this, see\n[Update the media metadata](/training/tv/playback/media-session#metadata).\n\nRespond to user action\n----------------------\n\n\nWhen the user selects the **Now Playing** card, the system opens the app that owns the\nsession. If your app provides a\n[PendingIntent](/reference/android/app/PendingIntent) to\n[setSessionActivity()](/reference/android/media/session/MediaSession#setSessionActivity(android.app.PendingIntent)),\nthe system launches the activity you specify, as shown in the following code snippet. If not,\nthe default system intent opens. The activity you specify must provide playback controls that\nlet users pause or stop playback. \n\n### Kotlin\n\n```kotlin\nval pi: PendingIntent = Intent(context, MyActivity::class.java).let { intent -\u003e\n PendingIntent.getActivity(\n context, 99 /*request code*/,\n intent,\n PendingIntent.FLAG_UPDATE_CURRENT\n )\n}\nsession.setSessionActivity(pi)\n```\n\n### Java\n\n```java\nIntent intent = new Intent(context, MyActivity.class);\nPendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/,\n intent, PendingIntent.FLAG_UPDATE_CURRENT);\nsession.setSessionActivity(pi);\n```\n\nAccepted use cases\n------------------\n\n\nThe **Now Playing** card should only be used in cases where the user expects audio to continue\nplaying in the background when leaving your app. Video playback or sound from a game should always\npause, unless your app is integrating and compliant with picture-in-picture."]]