ตัวสลับเอาต์พุต

ตัวสลับเอาต์พุตเป็นฟีเจอร์ของ Cast SDK ที่ช่วยให้การโอน ระหว่างการเล่นเนื้อหาในเครื่องและการเล่นจากระยะไกลเป็นไปอย่างราบรื่นโดยเริ่มตั้งแต่ Android 13 เป้าหมาย คือการช่วยให้แอปที่ส่งควบคุมตำแหน่งที่เล่นเนื้อหาได้อย่างง่ายดายและรวดเร็ว ตัวสลับเอาต์พุตใช้ไลบรารี MediaRouter เพื่อ สลับการเล่นเนื้อหาระหว่างลำโพงโทรศัพท์ อุปกรณ์บลูทูธที่จับคู่ไว้ และอุปกรณ์ที่พร้อมใช้งาน Cast ที่อยู่ระยะไกล กรณีการใช้งานแบ่งออกเป็นสถานการณ์ต่อไปนี้ได้

ดาวน์โหลดและใช้แอปตัวอย่าง CastVideos-android เพื่อเป็นข้อมูลอ้างอิงเกี่ยวกับวิธีใช้ตัวสลับเอาต์พุตในแอป

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

UI ตัวสลับเอาต์พุต

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

ปัญหาที่ทราบ

  • ระบบจะปิดและสร้าง Media Session ที่สร้างขึ้นสำหรับการเล่นในเครื่องใหม่ เมื่อเปลี่ยนไปใช้การแจ้งเตือนของ Cast SDK

จุดแรกเข้า

การแจ้งเตือนสื่อ

หากแอปโพสต์การแจ้งเตือนสื่อที่มี MediaSession สำหรับ การเล่นในเครื่อง (เล่นในเครื่อง) มุมขวาบนของการแจ้งเตือนสื่อ จะแสดงชิปการแจ้งเตือนพร้อมชื่ออุปกรณ์ (เช่น ลำโพงโทรศัพท์) ที่ กำลังเล่นเนื้อหาอยู่ การแตะชิปการแจ้งเตือนจะเปิด UI ของระบบกล่องโต้ตอบตัวสลับเอาต์พุต

การตั้งค่าระดับเสียง

นอกจากนี้ คุณยังเรียกใช้ UI ของระบบกล่องโต้ตอบตัวสลับเอาต์พุตได้โดยคลิก ปุ่มปรับระดับเสียงจริงบนอุปกรณ์ แตะไอคอนการตั้งค่าที่ด้านล่าง แล้วแตะข้อความ "เล่น [ชื่อแอป] บน [อุปกรณ์ Cast]"

สรุปขั้นตอน

ข้อกำหนดเบื้องต้น

  1. ย้ายข้อมูลแอป Android ที่มีอยู่ไปยัง AndroidX
  2. อัปเดต build.gradle ของแอปเพื่อใช้ Android Sender SDK สำหรับตัวสลับเอาต์พุตเวอร์ชันขั้นต่ำที่จำเป็น
    dependencies {
      ...
      implementation 'com.google.android.gms:play-services-cast-framework:21.2.0'
      ...
    }
  3. แอปนี้รองรับการแจ้งเตือนสื่อ
  4. อุปกรณ์ที่ใช้ Android 13

ตั้งค่าการแจ้งเตือนสื่อ

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

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

Kotlin
// Create a media session. NotificationCompat.MediaStyle
// PlayerService is your own Service or Activity responsible for media playback.
val mediaSession = MediaSessionCompat(this, "PlayerService")

// Create a MediaStyle object and supply your media session token to it.
val mediaStyle = Notification.MediaStyle().setMediaSession(mediaSession.sessionToken)

// Create a Notification which is styled by your MediaStyle object.
// This connects your media session to the media controls.
// Don't forget to include a small icon.
val notification = Notification.Builder(this@PlayerService, CHANNEL_ID)
    .setStyle(mediaStyle)
    .setSmallIcon(R.drawable.ic_app_logo)
    .build()

// Specify any actions which your users can perform, such as pausing and skipping to the next track.
val pauseAction: Notification.Action = Notification.Action.Builder(
        pauseIcon, "Pause", pauseIntent
    ).build()
notification.addAction(pauseAction)
Java
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    // Create a media session. NotificationCompat.MediaStyle
    // PlayerService is your own Service or Activity responsible for media playback.
    MediaSession mediaSession = new MediaSession(this, "PlayerService");

    // Create a MediaStyle object and supply your media session token to it.
    Notification.MediaStyle mediaStyle = new Notification.MediaStyle().setMediaSession(mediaSession.getSessionToken());

    // Specify any actions which your users can perform, such as pausing and skipping to the next track.
    Notification.Action pauseAction = Notification.Action.Builder(pauseIcon, "Pause", pauseIntent).build();

    // Create a Notification which is styled by your MediaStyle object.
    // This connects your media session to the media controls.
    // Don't forget to include a small icon.
    String CHANNEL_ID = "CHANNEL_ID";
    Notification notification = new Notification.Builder(this, CHANNEL_ID)
        .setStyle(mediaStyle)
        .setSmallIcon(R.drawable.ic_app_logo)
        .addAction(pauseAction)
        .build();
}

นอกจากนี้ หากต้องการป้อนข้อมูลสำหรับสื่อลงในการแจ้งเตือน คุณจะต้องเพิ่มข้อมูลเมตาและสถานะการเล่น ของสื่อลงใน MediaSession

หากต้องการเพิ่มข้อมูลเมตาลงใน MediaSession ให้ใช้ setMetaData() และระบุค่าคงที่ MediaMetadata ที่เกี่ยวข้องทั้งหมดสำหรับ สื่อใน MediaMetadataCompat.Builder()

Kotlin
mediaSession.setMetadata(MediaMetadataCompat.Builder()
    // Title
    .putString(MediaMetadata.METADATA_KEY_TITLE, currentTrack.title)

    // Artist
    // Could also be the channel name or TV series.
    .putString(MediaMetadata.METADATA_KEY_ARTIST, currentTrack.artist)

    // Album art
    // Could also be a screenshot or hero image for video content
    // The URI scheme needs to be "content", "file", or "android.resource".
    .putString(
        MediaMetadata.METADATA_KEY_ALBUM_ART_URI, currentTrack.albumArtUri)
    )

    // Duration
    // If duration isn't set, such as for live broadcasts, then the progress
    // indicator won't be shown on the seekbar.
    .putLong(MediaMetadata.METADATA_KEY_DURATION, currentTrack.duration)

    .build()
)
Java
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    mediaSession.setMetadata(
        new MediaMetadataCompat.Builder()
        // Title
        .putString(MediaMetadata.METADATA_KEY_TITLE, currentTrack.title)

        // Artist
        // Could also be the channel name or TV series.
        .putString(MediaMetadata.METADATA_KEY_ARTIST, currentTrack.artist)

        // Album art
        // Could also be a screenshot or hero image for video content
        // The URI scheme needs to be "content", "file", or "android.resource".
        .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, currentTrack.albumArtUri)

        // Duration
        // If duration isn't set, such as for live broadcasts, then the progress
        // indicator won't be shown on the seekbar.
        .putLong(MediaMetadata.METADATA_KEY_DURATION, currentTrack.duration)

        .build()
    );
}

หากต้องการเพิ่มสถานะการเล่นลงใน MediaSession ให้ใช้ setPlaybackState() และระบุค่าคงที่ที่เกี่ยวข้องทั้งหมดของ PlaybackStateCompat สื่อใน PlaybackStateCompat.Builder()

Kotlin
mediaSession.setPlaybackState(
    PlaybackStateCompat.Builder()
        .setState(
            PlaybackStateCompat.STATE_PLAYING,

            // Playback position
            // Used to update the elapsed time and the progress bar.
            mediaPlayer.currentPosition.toLong(),

            // Playback speed
            // Determines the rate at which the elapsed time changes.
            playbackSpeed
        )

        // isSeekable
        // Adding the SEEK_TO action indicates that seeking is supported
        // and makes the seekbar position marker draggable. If this is not
        // supplied seek will be disabled but progress will still be shown.
        .setActions(PlaybackStateCompat.ACTION_SEEK_TO)
        .build()
)
Java
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    mediaSession.setPlaybackState(
        new PlaybackStateCompat.Builder()
            .setState(
                 PlaybackStateCompat.STATE_PLAYING,

                // Playback position
                // Used to update the elapsed time and the progress bar.
                mediaPlayer.currentPosition.toLong(),

                // Playback speed
                // Determines the rate at which the elapsed time changes.
                playbackSpeed
            )

        // isSeekable
        // Adding the SEEK_TO action indicates that seeking is supported
        // and makes the seekbar position marker draggable. If this is not
        // supplied seek will be disabled but progress will still be shown.
        .setActions(PlaybackStateCompat.ACTION_SEEK_TO)
        .build()
    );
}

ลักษณะการทำงานของการแจ้งเตือนของแอปวิดีโอ

แอปวิดีโอหรือแอปเสียงที่ไม่รองรับการเล่นในเครื่องในเบื้องหลัง ควรมีลักษณะการทำงานที่เฉพาะเจาะจงสำหรับการแจ้งเตือนสื่อเพื่อหลีกเลี่ยงปัญหาเกี่ยวกับการ ส่งคำสั่งสื่อในสถานการณ์ที่ไม่รองรับการเล่น

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

เปิดใช้ตัวสลับเอาต์พุตใน AndroidManifest.xml

หากต้องการเปิดใช้ตัวสลับเอาต์พุต คุณต้องเพิ่ม MediaTransferReceiver ลงใน AndroidManifest.xml ของแอป หากไม่เป็นเช่นนั้น ระบบจะไม่เปิดใช้ฟีเจอร์ และฟีเจอร์แฟล็กจากระยะไกลไปยังเครื่องก็จะใช้ไม่ได้เช่นกัน

<application>
    ...
    <receiver
         android:name="androidx.mediarouter.media.MediaTransferReceiver"
         android:exported="true">
    </receiver>
    ...
</application>

MediaTransferReceiver เป็นตัวรับสัญญาณออกอากาศที่ช่วยให้โอนสื่อระหว่างอุปกรณ์ที่มี UI ของระบบได้ ดูข้อมูลเพิ่มเติมได้ที่การอ้างอิง MediaTransferReceiver

จากในพื้นที่ไปยังระยะไกล

เมื่อผู้ใช้เปลี่ยนการเล่นจากในเครื่องเป็นระยะไกล Cast SDK จะเริ่ม เซสชัน Cast โดยอัตโนมัติ อย่างไรก็ตาม แอปต้องจัดการการเปลี่ยนจาก การเล่นในเครื่องเป็นการเล่นระยะไกล เช่น หยุดการเล่นในเครื่อง และโหลดสื่อในอุปกรณ์ Cast แอปควรรับฟัง Cast SessionManagerListener โดยใช้ onSessionStarted() และ onSessionEnded() การเรียกกลับ และจัดการการดำเนินการเมื่อได้รับการเรียกกลับ Cast SessionManager แอปควรตรวจสอบว่าการเรียกกลับเหล่านี้ยังคงใช้งานได้เมื่อ เปิดกล่องโต้ตอบสวิตช์สลับเอาต์พุตและแอปไม่ได้ทำงานอยู่เบื้องหน้า

อัปเดต SessionManagerListener สำหรับการแคสต์ในเบื้องหลัง

ประสบการณ์การใช้งาน Cast แบบเดิมรองรับการแคสต์จากอุปกรณ์ในเครือข่ายเดียวกันไปยังอุปกรณ์ที่อยู่คนละเครือข่ายอยู่แล้วเมื่อแอปทำงานอยู่เบื้องหน้า โดยปกติแล้ว ประสบการณ์การแคสต์จะเริ่มต้นเมื่อผู้ใช้คลิกไอคอนแคสต์ ในแอปและเลือกอุปกรณ์ที่จะสตรีมสื่อ ในกรณีนี้ แอปต้อง ลงทะเบียนกับ SessionManagerListener ใน onCreate() หรือ onStart() และยกเลิกการลงทะเบียน Listener ใน onStop() หรือ onDestroy() ของกิจกรรมในแอป

ประสบการณ์การแคสต์แบบใหม่โดยใช้ตัวสลับเอาต์พุตช่วยให้แอปเริ่ม แคสต์ได้เมื่อแอปทำงานในเบื้องหลัง ซึ่งจะมีประโยชน์อย่างยิ่งสำหรับแอปเสียงที่โพสต์การแจ้งเตือนเมื่อเล่นในเบื้องหลัง แอปสามารถลงทะเบียน เครื่องฟัง SessionManager ใน onCreate() ของบริการและยกเลิกการลงทะเบียนใน onDestroy() ของบริการ แอปควรได้รับการเรียกกลับจากเครื่องไปยังระยะไกลเสมอ (เช่น onSessionStarted) เมื่อแอปอยู่ในเบื้องหลัง

หากแอปใช้ MediaBrowserService ขอแนะนำให้ลงทะเบียน SessionManagerListener ที่นั่น

Kotlin
class MyService : Service() {
    private var castContext: CastContext? = null
    protected fun onCreate() {
        castContext = CastContext.getSharedInstance(this)
        castContext
            .getSessionManager()
            .addSessionManagerListener(sessionManagerListener, CastSession::class.java)
    }

    protected fun onDestroy() {
        if (castContext != null) {
            castContext
                .getSessionManager()
                .removeSessionManagerListener(sessionManagerListener, CastSession::class.java)
        }
    }
}
Java
public class MyService extends Service {
  private CastContext castContext;

  @Override
  protected void onCreate() {
     castContext = CastContext.getSharedInstance(this);
     castContext
        .getSessionManager()
        .addSessionManagerListener(sessionManagerListener, CastSession.class);
  }

  @Override
  protected void onDestroy() {
    if (castContext != null) {
       castContext
          .getSessionManager()
          .removeSessionManagerListener(sessionManagerListener, CastSession.class);
    }
  }
}

การอัปเดตนี้ทำให้การแคสต์จากอุปกรณ์ในพื้นที่ไปยังอุปกรณ์ระยะไกลทำงานเหมือนกับการแคสต์ทั่วไปเมื่อแอปทำงานในเบื้องหลัง และไม่จำเป็นต้องดำเนินการเพิ่มเติมเพื่อเปลี่ยนจากอุปกรณ์บลูทูธเป็นอุปกรณ์ Cast

รีโมตเป็นโลคัล

ตัวสลับเอาต์พุตช่วยให้คุณเปลี่ยนจากการเล่นระยะไกลไปเป็น ลำโพงโทรศัพท์หรืออุปกรณ์บลูทูธในพื้นที่ได้ คุณเปิดใช้ได้โดยตั้งค่า Flag setRemoteToLocalEnabled เป็น true ใน CastOptions

ในกรณีที่อุปกรณ์ของผู้ส่งปัจจุบันเข้าร่วมเซสชันที่มีอยู่ซึ่งมีผู้ส่งหลายราย และแอปต้องตรวจสอบว่าระบบอนุญาตให้โอนสื่อปัจจุบันในเครื่องหรือไม่ แอปควรใช้แฮนเดิล onTransferred ของ SessionTransferCallback เพื่อตรวจสอบ SessionState

ตั้งค่าแฟล็ก setRemoteToLocalEnabled

CastOptions.Builder มีsetRemoteToLocalEnabledสำหรับแสดงหรือซ่อนลำโพงโทรศัพท์และอุปกรณ์บลูทูธในพื้นที่เป็นเป้าหมายการโอน ในกล่องโต้ตอบตัวสลับเอาต์พุตเมื่อมีเซสชันการแคสต์ที่ใช้งานอยู่

Kotlin
class CastOptionsProvider : OptionsProvider {
    fun getCastOptions(context: Context?): CastOptions {
        ...
        return Builder()
            ...
            .setRemoteToLocalEnabled(true)
            .build()
    }
}
Java
public class CastOptionsProvider implements OptionsProvider {
    @Override
    public CastOptions getCastOptions(Context context) {
        ...
        return new CastOptions.Builder()
            ...
            .setRemoteToLocalEnabled(true)
            .build()
  }
}

เล่นต่อในเครื่อง

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

CastContext#addSessionTransferCallback(SessionTransferCallback) อนุญาตให้แอปบันทึก SessionTransferCallback และรอรับการเรียกกลับ onTransferred และ onTransferFailed เมื่อมีการโอนผู้ส่ง ไปยังการเล่นในเครื่อง

หลังจากที่แอปยกเลิกการลงทะเบียน SessionTransferCallback แล้ว แอปจะไม่ได้รับ SessionTransferCallback อีกต่อไป

SessionTransferCallback เป็นส่วนขยายของSessionManagerListener การเรียกกลับที่มีอยู่ และจะทริกเกอร์หลังจากที่onSessionEnded ทริกเกอร์ ลำดับของ การเรียกกลับจากระยะไกลไปยังเครื่องมีดังนี้

  1. onTransferring
  2. onSessionEnding
  3. onSessionEnded
  4. onTransferred

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

แอปที่รองรับการเล่นอยู่เบื้องหลัง

สำหรับแอปที่รองรับการเล่นในเบื้องหลัง (โดยปกติคือแอปเสียง) เราขอแนะนำให้ใช้ Service (เช่น MediaBrowserService) บริการควรรับฟังการเรียกกลับ onTransferred และเล่นต่อในเครื่องเมื่อแอปอยู่ในเบื้องหน้าหรือเบื้องหลัง

Kotlin
class MyService : Service() {
    private var castContext: CastContext? = null
    private var sessionTransferCallback: SessionTransferCallback? = null
    protected fun onCreate() {
        castContext = CastContext.getSharedInstance(this)
        castContext.getSessionManager()
                   .addSessionManagerListener(sessionManagerListener, CastSession::class.java)
        sessionTransferCallback = MySessionTransferCallback()
        castContext.addSessionTransferCallback(sessionTransferCallback)
    }

    protected fun onDestroy() {
        if (castContext != null) {
            castContext.getSessionManager()
                       .removeSessionManagerListener(sessionManagerListener, CastSession::class.java)
            if (sessionTransferCallback != null) {
                castContext.removeSessionTransferCallback(sessionTransferCallback)
            }
        }
    }

    class MySessionTransferCallback : SessionTransferCallback() {
        fun onTransferring(@SessionTransferCallback.TransferType transferType: Int) {
            // Perform necessary steps prior to onTransferred
        }

        fun onTransferred(@SessionTransferCallback.TransferType transferType: Int,
                          sessionState: SessionState?) {
            if (transferType == SessionTransferCallback.TRANSFER_TYPE_FROM_REMOTE_TO_LOCAL) {
                // Remote stream is transferred to the local device.
                // Retrieve information from the SessionState to continue playback on the local player.
            }
        }

        fun onTransferFailed(@SessionTransferCallback.TransferType transferType: Int,
                             @SessionTransferCallback.TransferFailedReason transferFailedReason: Int) {
            // Handle transfer failure.
        }
    }
}
Java
public class MyService extends Service {
    private CastContext castContext;
    private SessionTransferCallback sessionTransferCallback;

    @Override
    protected void onCreate() {
        castContext = CastContext.getSharedInstance(this);
        castContext.getSessionManager()
                   .addSessionManagerListener(sessionManagerListener, CastSession.class);
        sessionTransferCallback = new MySessionTransferCallback();
        castContext.addSessionTransferCallback(sessionTransferCallback);
    }

    @Override
    protected void onDestroy() {
        if (castContext != null) {
            castContext.getSessionManager()
                       .removeSessionManagerListener(sessionManagerListener, CastSession.class);
            if (sessionTransferCallback != null) {
                castContext.removeSessionTransferCallback(sessionTransferCallback);
            }
        }
    }

    public static class MySessionTransferCallback extends SessionTransferCallback {
        public MySessionTransferCallback() {}

        @Override
        public void onTransferring(@SessionTransferCallback.TransferType int transferType) {
            // Perform necessary steps prior to onTransferred
        }

        @Override
        public void onTransferred(@SessionTransferCallback.TransferType int transferType,
                                  SessionState sessionState) {
            if (transferType==SessionTransferCallback.TRANSFER_TYPE_FROM_REMOTE_TO_LOCAL) {
                // Remote stream is transferred to the local device.
                // Retrieve information from the SessionState to continue playback on the local player.
            }
        }

        @Override
        public void onTransferFailed(@SessionTransferCallback.TransferType int transferType,
                                     @SessionTransferCallback.TransferFailedReason int transferFailedReason) {
            // Handle transfer failure.
        }
    }
}

แอปที่ไม่รองรับการเล่นอยู่เบื้องหลัง

สำหรับแอปที่ไม่รองรับการเล่นในเบื้องหลัง (โดยปกติคือแอปวิดีโอ) เราขอแนะนำให้คุณฟังonTransferred การเรียกกลับและเล่นต่อในเครื่องหากแอปอยู่เบื้องหน้า

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

Kotlin
class MyActivity : AppCompatActivity() {
    private var castContext: CastContext? = null
    private var sessionTransferCallback: SessionTransferCallback? = null
    protected fun onCreate() {
        castContext = CastContext.getSharedInstance(this)
        castContext.getSessionManager()
                   .addSessionManagerListener(sessionManagerListener, CastSession::class.java)
        sessionTransferCallback = MySessionTransferCallback()
        castContext.addSessionTransferCallback(sessionTransferCallback)
    }

    protected fun onDestroy() {
        if (castContext != null) {
            castContext.getSessionManager()
                       .removeSessionManagerListener(sessionManagerListener, CastSession::class.java)
            if (sessionTransferCallback != null) {
                castContext.removeSessionTransferCallback(sessionTransferCallback)
            }
        }
    }

    class MySessionTransferCallback : SessionTransferCallback() {
        fun onTransferring(@SessionTransferCallback.TransferType transferType: Int) {
            // Perform necessary steps prior to onTransferred
        }

        fun onTransferred(@SessionTransferCallback.TransferType transferType: Int,
                          sessionState: SessionState?) {
            if (transferType == SessionTransferCallback.TRANSFER_TYPE_FROM_REMOTE_TO_LOCAL) {
                // Remote stream is transferred to the local device.

                // Retrieve information from the SessionState to continue playback on the local player.
            }
        }

        fun onTransferFailed(@SessionTransferCallback.TransferType transferType: Int,
                             @SessionTransferCallback.TransferFailedReason transferFailedReason: Int) {
            // Handle transfer failure.
        }
    }
}
Java
public class MyActivity extends AppCompatActivity {
  private CastContext castContext;
  private SessionTransferCallback sessionTransferCallback;

  @Override
  protected void onCreate() {
     castContext = CastContext.getSharedInstance(this);
     castContext
        .getSessionManager()
        .addSessionManagerListener(sessionManagerListener, CastSession.class);
     sessionTransferCallback = new MySessionTransferCallback();
     castContext.addSessionTransferCallback(sessionTransferCallback);
  }

  @Override
  protected void onDestroy() {
    if (castContext != null) {
       castContext
          .getSessionManager()
          .removeSessionManagerListener(sessionManagerListener, CastSession.class);
      if (sessionTransferCallback != null) {
         castContext.removeSessionTransferCallback(sessionTransferCallback);
      }
    }
  }

  public static class MySessionTransferCallback extends SessionTransferCallback {
    public MySessionTransferCallback() {}

    @Override
    public void onTransferring(@SessionTransferCallback.TransferType int transferType) {
        // Perform necessary steps prior to onTransferred
    }

    @Override
    public void onTransferred(@SessionTransferCallback.TransferType int transferType,
                               SessionState sessionState) {
      if (transferType==SessionTransferCallback.TRANSFER_TYPE_FROM_REMOTE_TO_LOCAL) {
        // Remote stream is transferred to the local device.

        // Retrieve information from the SessionState to continue playback on the local player.
      }
    }

    @Override
    public void onTransferFailed(@SessionTransferCallback.TransferType int transferType,
                                 @SessionTransferCallback.TransferFailedReason int transferFailedReason) {
      // Handle transfer failure.
    }
  }
}

จากระยะไกลถึงระยะไกล

ตัวสลับเอาต์พุตรองรับความสามารถในการขยายไปยังอุปกรณ์ลำโพงหลายเครื่องที่พร้อมใช้งาน Cast สำหรับแอปเสียงที่ใช้การขยายสตรีม

แอปเสียงคือแอปที่รองรับ Google Cast สำหรับเสียงในการตั้งค่าแอปตัวรับ ในแผงควบคุมสำหรับนักพัฒนาซอฟต์แวร์ Google Cast SDK

การขยายสตรีมด้วยลำโพง

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

แพลตฟอร์ม Cast รองรับฟีเจอร์นี้ และไม่จำเป็นต้องทำการเปลี่ยนแปลงเพิ่มเติม หากแอปใช้ UI เริ่มต้น หากใช้ UI ที่กำหนดเอง แอป ควรอัปเดต UI เพื่อแสดงว่าแอปกำลังแคสต์ไปยังกลุ่ม

หากต้องการรับชื่อกลุ่มใหม่ที่ขยายแล้วระหว่างการขยายสตรีม ให้ลงทะเบียน Cast.Listener โดยใช้ CastSession#addCastListener จากนั้นโทร CastSession#getCastDevice() ระหว่างการโทรกลับของ onDeviceNameChanged

Kotlin
class MyActivity : Activity() {
    private var mCastSession: CastSession? = null
    private lateinit var mCastContext: CastContext
    private lateinit var mSessionManager: SessionManager
    private val mSessionManagerListener: SessionManagerListener<CastSession> =
        SessionManagerListenerImpl()
    private val mCastListener = CastListener()

    private inner class SessionManagerListenerImpl : SessionManagerListener<CastSession?> {
        override fun onSessionStarting(session: CastSession?) {}

        override fun onSessionStarted(session: CastSession?, sessionId: String) {
            addCastListener(session)
        }

        override fun onSessionStartFailed(session: CastSession?, error: Int) {}

        override fun onSessionSuspended(session: CastSession?, reason Int) {
            removeCastListener()
        }

        override fun onSessionResuming(session: CastSession?, sessionId: String) {}

        override fun onSessionResumed(session: CastSession?, wasSuspended: Boolean) {
            addCastListener(session)
        }

        override fun onSessionResumeFailed(session: CastSession?, error: Int) {}

        override fun onSessionEnding(session: CastSession?) {}

        override fun onSessionEnded(session: CastSession?, error: Int) {
            removeCastListener()
        }
    }

    private inner class CastListener : Cast.Listener() {
        override fun onDeviceNameChanged() {
            mCastSession?.let {
                val castDevice = it.castDevice
                val deviceName = castDevice.friendlyName
                // Update UIs with the new cast device name.
            }
        }
    }

    private fun addCastListener(castSession: CastSession) {
        mCastSession = castSession
        mCastSession?.addCastListener(mCastListener)
    }

    private fun removeCastListener() {
        mCastSession?.removeCastListener(mCastListener)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mCastContext = CastContext.getSharedInstance(this)
        mSessionManager = mCastContext.sessionManager
        mSessionManager.addSessionManagerListener(mSessionManagerListener, CastSession::class.java)
    }

    override fun onDestroy() {
        super.onDestroy()
        mSessionManager.removeSessionManagerListener(mSessionManagerListener, CastSession::class.java)
    }
}
Java
public class MyActivity extends Activity {
    private CastContext mCastContext;
    private CastSession mCastSession;
    private SessionManager mSessionManager;
    private SessionManagerListener<CastSession> mSessionManagerListener =
            new SessionManagerListenerImpl();
    private Cast.Listener mCastListener = new CastListener();

    private class SessionManagerListenerImpl implements SessionManagerListener<CastSession> {
        @Override
        public void onSessionStarting(CastSession session) {}
        @Override
        public void onSessionStarted(CastSession session, String sessionId) {
            addCastListener(session);
        }
        @Override
        public void onSessionStartFailed(CastSession session, int error) {}
        @Override
        public void onSessionSuspended(CastSession session, int reason) {
            removeCastListener();
        }
        @Override
        public void onSessionResuming(CastSession session, String sessionId) {}
        @Override
        public void onSessionResumed(CastSession session, boolean wasSuspended) {
            addCastListener(session);
        }
        @Override
        public void onSessionResumeFailed(CastSession session, int error) {}
        @Override
        public void onSessionEnding(CastSession session) {}
        @Override
        public void onSessionEnded(CastSession session, int error) {
            removeCastListener();
        }
    }

    private class CastListener extends Cast.Listener {
         @Override
         public void onDeviceNameChanged() {
             if (mCastSession == null) {
                 return;
             }
             CastDevice castDevice = mCastSession.getCastDevice();
             String deviceName = castDevice.getFriendlyName();
             // Update UIs with the new cast device name.
         }
    }

    private void addCastListener(CastSession castSession) {
        mCastSession = castSession;
        mCastSession.addCastListener(mCastListener);
    }

    private void removeCastListener() {
        if (mCastSession != null) {
            mCastSession.removeCastListener(mCastListener);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCastContext = CastContext.getSharedInstance(this);
        mSessionManager = mCastContext.getSessionManager();
        mSessionManager.addSessionManagerListener(mSessionManagerListener, CastSession.class);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mSessionManager.removeSessionManagerListener(mSessionManagerListener, CastSession.class);
    }
}

การทดสอบจากระยะไกลถึงระยะไกล

วิธีทดสอบฟีเจอร์

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