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 ↗Code shown on screen · 5 snippets
Define a GroupActivity
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
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
// Receiving a GroupSession from the GroupSession AsyncSequence
func listenForGroupSession() {
Task {
for await session in MovieWatchingActivity.sessions() {
...
}
}
} Attaching an AVPlayer to the GroupSession
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
class AVPlaybackCoordinator {
func beginSuspension(for reason: AVCoordinatedPlaybackSuspension.Reason) -> AVCoordinatedPlaybackSuspension
}
class AVCoordinatedPlaybackSuspension {
func end()
func end(proposingNewTime newTime: CMTime)
} Resources
Related sessions
-
19 min -
15 min -
17 min -
25 min -
12 min