แสดงการ์ด "กำลังเล่น"
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แอปทีวีที่เปิดเสียงอาจเล่นต่อไปหลังจากที่ผู้ใช้กลับไปที่หน้าจอหลัก หรือ
จะเปลี่ยนไปใช้แอปอื่น หากต้องการทำเช่นนั้น แอปต้องมีการ์ดกำลังเล่นในบ้าน
บนหน้าจอ การ์ดนี้ช่วยให้ผู้ใช้ทราบว่าเสียงมาจากไหนและกลับไปยังแอปเพื่อ
ควบคุมการเล่นสื่อ
เมื่อใดก็ตามที่มีการใช้งาน
MediaSession
คือ
เฟรมเวิร์กของ Android จะแสดงการ์ดกำลังเล่นบนหน้าจอหลัก การ์ด
ประกอบด้วยข้อมูลเมตาของสื่อ เช่น ปกอัลบั้ม ชื่อ และไอคอนแอป เมื่อผู้ใช้เลือกการ์ด
ระบบจะเปิดแอป
การ์ด "กำลังเล่น"
หลังจากที่คุณใช้เซสชันสื่อ ให้ตั้งค่า
เซสชันเพื่อใช้งานอยู่ และขอโฟกัสเสียง การ์ดกำลังเล่นจะปรากฏขึ้น
หมายเหตุ: การ์ดกำลังเล่นจะแสดงสำหรับสื่อเท่านั้น
กับ
FLAG_HANDLES_TRANSPORT_CONTROLS
ตั้งค่าแฟล็กแล้ว เลิกใช้งานแฟล็กนี้ใน API ระดับ 26 แล้ว อย่างไรก็ตาม อาจยังต้องใช้ Flag นี้ในอุปกรณ์รุ่นเก่าเพื่อให้เข้ากันได้กับอุปกรณ์รุ่นเก่า
การ์ดจะถูกนำออกจากหน้าจอ Launcher เมื่อ
setActive(false)
การเรียกจะเป็นการปิดใช้งานเซสชันสื่อหรือเมื่อแอปอื่นเริ่มเล่นสื่อ หากการเล่นคือ
หยุดโดยสมบูรณ์และไม่มีสื่อที่ใช้งานอยู่ ให้ปิดใช้งานเซสชันสื่อ
ทันที หากการเล่นหยุดชั่วคราว ให้ปิดใช้งานเซสชันสื่อหลังจากการหน่วงเวลา
คือ 5-30 นาที
อัปเดตบัตร
เมื่อใดก็ตามที่แอปอัปเดตสถานะการเล่นใน MediaSession
การ์ดกำลังเล่นจะอัปเดตเพื่อแสดงสถานะของสื่อปัจจุบัน ดูวิธีการได้ที่
อัปเดตสถานะการเล่น
ในทำนองเดียวกัน แอปของคุณสามารถอัปเดต
MediaMetadata
เพื่อระบุข้อมูล
ลงในการ์ดกำลังเล่นเกี่ยวกับสื่อปัจจุบัน เช่น ชื่อ ชื่อรอง
และไอคอนต่างๆ ดูวิธีการได้ที่
อัปเดตข้อมูลเมตาของสื่อ
ตอบสนองต่อการดำเนินการของผู้ใช้
เมื่อผู้ใช้เลือกการ์ดกำลังเล่น ระบบจะเปิดแอปที่เป็นเจ้าของ
เซสชัน หากแอปของคุณมี
PendingIntent
ถึง
setSessionActivity()
,
ระบบจะเปิดกิจกรรมที่คุณระบุ ดังที่ปรากฏในข้อมูลโค้ดต่อไปนี้ หากไม่เป็นเช่นนั้น
Intent เริ่มต้นของระบบจะเปิดขึ้น กิจกรรมที่คุณระบุต้องมีตัวควบคุมการเล่นที่
ช่วยให้ผู้ใช้หยุดชั่วคราวหรือหยุดเล่นได้
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);
Use Case ที่ยอมรับ
ควรใช้การ์ดกำลังเล่นในกรณีที่ผู้ใช้คาดหวังให้เล่นเสียงต่อเท่านั้น
เล่นขณะล็อกหน้าจอหรือขณะใช้แอปอื่นเมื่อออกจากแอป การเล่นวิดีโอหรือเสียงจากเกมควร
หยุดชั่วคราว เว้นแต่แอปของคุณจะผสานรวมและเป็นไปตามข้อกำหนดการแสดงภาพซ้อนภาพ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],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."]]