เชื่อมต่อกับแอปสื่อ

ตัวควบคุมสื่อจะโต้ตอบกับเซสชันสื่อเพื่อค้นหาและควบคุมการเล่นของแอปสื่อ ใน Media3 MediaController API จะใช้Playerอินเทอร์เฟซ ตัวอย่างแอปไคลเอ็นต์ที่ใช้ตัวควบคุมสื่อมีดังนี้

นอกจากนี้ ตัวควบคุมสื่อยังมีประโยชน์ในแอปสื่อด้วย เช่น หากเพลเยอร์และเซสชันสื่ออยู่ในServiceแยกจากActivityหรือFragmentที่มี UI

สร้าง MediaController

หากต้องการสร้าง MediaController ให้เริ่มด้วยการสร้าง SessionToken สำหรับ MediaSession ที่เกี่ยวข้อง onStart() วิธีการของActivityหรือ Fragment อาจเป็นจุดเริ่มต้นที่ดี

Kotlin

val sessionToken =
  SessionToken(context, ComponentName(context, PlaybackService::class.java))

Java

SessionToken sessionToken =
  new SessionToken(context, new ComponentName(context, PlaybackService.class));

การใช้ SessionToken นี้เพื่อสร้าง MediaController จะเชื่อมต่อ คอนโทรลเลอร์กับเซสชันที่ระบุ การดำเนินการนี้จะเกิดขึ้นแบบไม่พร้อมกัน ดังนั้นคุณควร รอฟังผลลัพธ์และใช้เมื่อพร้อมใช้งาน

Kotlin

val controllerFuture =
  MediaController.Builder(context, sessionToken).buildAsync()
controllerFuture.addListener({
  // MediaController is available here with controllerFuture.get()
}, MoreExecutors.directExecutor())

Java

ListenableFuture<MediaController> controllerFuture =
  new MediaController.Builder(context, sessionToken).buildAsync();
controllerFuture.addListener(() -> {
  // MediaController is available here with controllerFuture.get()
}, MoreExecutors.directExecutor());

ใช้ MediaController

MediaController ใช้Playerอินเทอร์เฟซ คุณจึงใช้คำสั่ง ที่กำหนดไว้ในอินเทอร์เฟซเพื่อควบคุมการเล่นของ MediaSession ที่เชื่อมต่อได้ กล่าวคือ การเรียกใช้ play() ใน MediaController จะส่งคำสั่งไปยัง MediaSession ที่เชื่อมต่ออยู่ ซึ่งจากนั้นจะมอบสิทธิ์คำสั่งไปยัง Player ที่อยู่เบื้องหลัง

คุณเพิ่ม Player.Listener ลงในตัวควบคุมเพื่อฟังการเปลี่ยนแปลงในสถานะ Player ได้ ดูรายละเอียดเพิ่มเติมเกี่ยวกับการใช้ Player.Listenerได้ในคู่มือเหตุการณ์ของผู้เล่น

อินเทอร์เฟซ MediaController.Listener จะกําหนดการเรียกกลับเพิ่มเติมสําหรับเหตุการณ์ และคําสั่งที่กําหนดเองจาก MediaSession ที่เชื่อมต่อ ตัวอย่างเช่น onCustomCommand() เมื่อเซสชันส่งคำสั่งที่กำหนดเอง onAvailableSessionCommandsChanged() เมื่อเซสชันเปลี่ยนคำสั่งเซสชันที่ใช้ได้ หรือ onDisconnected() เมื่อตัวควบคุมถูกตัดการเชื่อมต่อ จากเซสชัน

คุณตั้งค่า MediaController.Listener ได้เมื่อสร้างตัวควบคุมด้วย Builder

Kotlin

MediaController.Builder(context, sessionToken)
    .setListener(
      object : MediaController.Listener {
        override fun onCustomCommand(
          controller: MediaController,
          command: SessionCommand,
          args: Bundle,
        ): ListenableFuture<SessionResult> {
          // Handle custom command.
          return Futures.immediateFuture(SessionResult(SessionResult.RESULT_SUCCESS))
        }

        override fun onDisconnected(controller: MediaController) {
          // Handle disconnection.
        }
      }
    )
    .buildAsync()

Java

new MediaController.Builder(context, sessionToken)
    .setListener(
        new MediaController.Listener() {
          @Override
          public ListenableFuture<SessionResult> onCustomCommand(
              MediaController controller, SessionCommand command, Bundle args) {
            // Handle custom command.
            return Futures.immediateFuture(new SessionResult(SessionResult.RESULT_SUCCESS));
          }

          @Override
          public void onDisconnected(MediaController controller) {
            // Handle disconnection.
          }
        })
    .buildAsync();

เช่นเดียวกับคอมโพเนนต์อื่นๆ โปรดอย่าลืมปล่อย MediaController เมื่อไม่จำเป็นต้องใช้แล้ว เช่น ในเมธอด onStop() ของ Activity หรือ Fragment

Kotlin

MediaController.releaseFuture(controllerFuture)

Java

MediaController.releaseFuture(controllerFuture);

การปล่อยตัวควบคุมจะยังคงส่งคำสั่งที่รอดำเนินการทั้งหมดที่ส่งไปยังเซสชัน และจะยกเลิกการเชื่อมโยงจากบริการเซสชันเมื่อมีการจัดการคำสั่งเหล่านี้แล้วหรือหลังจากระยะหมดเวลา แล้วแต่ว่ากรณีใดจะเกิดขึ้นก่อน

สร้างและใช้ MediaBrowser

MediaBrowser สร้างขึ้นจากความสามารถที่ MediaController มีอยู่เพื่อเปิดใช้การเรียกดูคลังสื่อที่ MediaLibraryService ของแอปสื่อมีให้ด้วย

Kotlin

val browserFuture = MediaBrowser.Builder(context, sessionToken).buildAsync()
browserFuture.addListener({
  // MediaBrowser is available here with browserFuture.get()
}, MoreExecutors.directExecutor())

Java

ListenableFuture<MediaBrowser> browserFuture =
  new MediaBrowser.Builder(context, sessionToken).buildAsync();
browserFuture.addListener(() -> {
  // MediaBrowser is available here with browserFuture.get()
}, MoreExecutors.directExecutor());

หากต้องการเริ่มเรียกดูคลังเนื้อหาของแอปสื่อ ให้เรียกข้อมูลโหนดรูทก่อน ด้วย getLibraryRoot():

Kotlin

// Get the library root to start browsing the library tree.
val rootFuture = mediaBrowser.getLibraryRoot(/* params= */ null)
rootFuture.addListener({
  // Root node MediaItem is available here with rootFuture.get().value
}, MoreExecutors.directExecutor())

Java

// Get the library root to start browsing the library tree.
ListenableFuture<LibraryResult<MediaItem>> rootFuture =
  mediaBrowser.getLibraryRoot(/* params= */ null);
rootFuture.addListener(() -> {
  // Root node MediaItem is available here with rootFuture.get().value
}, MoreExecutors.directExecutor());

จากนั้นคุณจะไปยังส่วนต่างๆ ของคลังสื่อได้โดยการดึงข้อมูลรายการย่อยของ MediaItem ในคลังด้วย getChildren() เช่น หากต้องการดึงข้อมูล ลูกของโหนดรูท MediaItem

Kotlin

// Get the library root to start browsing the library tree.
val childrenFuture = 
  mediaBrowser.getChildren(rootMediaItem.mediaId, 0, Int.MAX_VALUE, null)
childrenFuture.addListener({
  // List of children MediaItem nodes is available here with
  // childrenFuture.get().value
}, MoreExecutors.directExecutor())

Java

ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> childrenFuture =
  mediaBrowser.getChildren(rootMediaItem.mediaId, 0, Integer.MAX_VALUE, null);
childrenFuture.addListener(() -> {
  // List of children MediaItem nodes is available here with
  // childrenFuture.get().value
}, MoreExecutors.directExecutor());

แสดงตัวควบคุมการเล่นสำหรับแอปสื่ออื่น

เมื่อแสดงตัวควบคุม UI พร้อมปุ่มสำหรับแอปสื่ออื่น คุณควรทำตามค่ากำหนดของปุ่มสื่อที่ประกาศไว้ของแอปนั้น

วิธีที่ดีที่สุดในการแก้ปัญหาค่ากำหนดของแอปและข้อจำกัดและข้อกำหนดของ UI คือการใช้ CommandButton.DisplayConstraints คุณสามารถ กำหนดขีดจำกัดและข้อจำกัดของสิ่งที่ UI ทำได้ และเมธอด resolve จะแสดงรายการปุ่มที่แน่นอนเพื่อ แสดงพร้อมไอคอน ตำแหน่ง และการดำเนินการที่ต้องการ