顯示「聽聲辨曲」資訊卡
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在使用者返回主畫面或
就會切換至其他應用程式。前提是應用程式必須在主畫面提供「現正播放」資訊卡
。這張資訊卡可讓使用者瞭解音訊來源,並返回您的應用程式
控制媒體播放。
每當有
MediaSession
是
Android 架構的主畫面會顯示「聽聲辨曲」資訊卡。資訊卡
包含媒體中繼資料,例如專輯封面、名稱和應用程式圖示。使用者選取資訊卡時
系統會開啟應用程式
「聽聲辨曲」資訊卡
導入媒體工作階段後,請設定
啟動並要求音訊焦點,畫面上會顯示「聽聲辨曲」資訊卡。
注意:「聽聲辨曲」資訊卡只會顯示特定媒體相關資料
與
FLAG_HANDLES_TRANSPORT_CONTROLS
旗標已設定。這個旗標已在 API 級別 26 淘汰。不過,為了維持回溯相容性,舊型裝置可能仍須使用這個標記。
發生下列情況時,系統會將資訊卡從啟動器畫面中移除
setActive(false)
呼叫會停用媒體工作階段,或其他應用程式啟動媒體播放。如果播放為
媒體工作階段完全停止,而且沒有使用中的媒體,請停用媒體工作階段
立即生效如果暫停播放,請在延遲後停用媒體工作階段,
通常從 5 到 30 分鐘
更新卡片
每當應用程式更新 MediaSession
中的播放狀態時,
「聽聲辨曲」資訊卡會更新,顯示目前媒體的狀態。如要瞭解操作方法,請參閱:
更新播放狀態。
同樣地,您的應用程式也可以更新
要提供 MediaMetadata
提供目前媒體的「聽聲辨曲」資訊卡相關資訊,例如片名、副標題
各種不同的圖示如要瞭解操作方法,請參閱:
更新媒體中繼資料。
回應使用者動作
使用者選取「聽聲辨曲」資訊卡時,系統會開啟擁有該應用程式的應用程式
會很有幫助如果您的應用程式提供
PendingIntent
到
setSessionActivity()
,
系統會啟動您指定的活動,如以下程式碼片段所示。如果不是
就會開啟預設的系統意圖您指定的活動必須提供播放控制項,
可讓使用者暫停或停止播放。
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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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."]]