Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2021 Audio & Video

WWDC21 · 38 min · Audio & Video

Coordinate media experiences with Group Activities

Discover how you can help people watch or listen to content all in sync with SharePlay and the Group Activities framework. We’ll show you how to adapt a media app into a synchronized, SharePlay-enabled experience for multiple people. Learn how to add Group Activities to your app, explore the Picture in Picture layout, and find out how the playback coordinator object can help you fine-tune playback across multiple devices.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 5 snippets

Define a GroupActivity swift · at 3:06 ↗
protocol GroupActivity: Codable {

    /// An identifier so the system knows how to reference this activity
    static var activityIdentifier: String { get }

    /// Information that the system uses to show this activity, such as title and a preview image
    var metadata: GroupActivityMetadata { get async }
}
Making your play buttons automatically start a group session when appropriate swift · at 4:42 ↗
func playButtonTapped() {
    let activity = MovieWatchingActivity(movie: movie)
            
    Task {
        switch await activity.prepareForActivation() {
        case .activationDisabled:
            // Playback coordination isn't active. Queue movie
            // for local playback.
            self.enqueuedMovie = movie
        case .activationPreferred:
            // Activate the activity. The system enqueues the movie
            // when the activity starts.
            activity.activate()
        case .cancelled:
            // The user cancelled the operation. Nothing to perform.
            break
        default:
            break
        }
    }
}
Receiving a GroupSession from the GroupSession AsyncSequence swift · at 8:31 ↗
// Receiving a GroupSession from the GroupSession AsyncSequence

func listenForGroupSession() {
    Task {
        for await session in MovieWatchingActivity.sessions() {
            ...
        }
    }
}
Attaching an AVPlayer to the GroupSession swift · at 9:03 ↗
let player = AVPlayer()

...

func listenForGroupSession() {
    Task {
        for await groupSession in MovieWatchingActivity.sessions() {
            
            // Verify content is available, prepare for playback to begin

            player.playbackCoordinator.coordinateWithSession(groupSession)

            ...
        }
    }
}
Custom suspensions swift · at 31:26 ↗
class AVPlaybackCoordinator {
    func beginSuspension(for reason: AVCoordinatedPlaybackSuspension.Reason) -> AVCoordinatedPlaybackSuspension
}

class AVCoordinatedPlaybackSuspension { 	
    func end()
    func end(proposingNewTime newTime: CMTime)
}

Resources