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

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

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

สร้าง MediaController

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

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

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

KotlinJava
val controllerFuture =
  MediaController.Builder(context, sessionToken).buildAsync()
controllerFuture.addListener({
  // MediaController is available here with controllerFuture.get()
}, MoreExecutors.directExecutor())
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 ที่เชื่อมต่อ เช่น onAvailableSessionCommandsChanged() สำหรับกรณีที่เซสชันสื่อเปลี่ยนคำสั่งเซสชันที่ใช้ได้ และ onDisconnected() สำหรับกรณีที่ตัวควบคุมถูกตัดการเชื่อมต่อจากเซสชัน

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

KotlinJava
MediaController.releaseFuture(controllerFuture)
MediaController.releaseFuture(controllerFuture);

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

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

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

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

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

KotlinJava
// 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())
// 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 ให้ทำดังนี้

KotlinJava
// 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())
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());