activate(options:completionHandler:)
Activates an audio session asynchronously.
Declaration
func activate(options: AVAudioSessionActivationOptions = [], completionHandler handler: @escaping @Sendable (Bool, (any Error)?) -> Void)func activate(options: AVAudioSessionActivationOptions = []) async throws -> BoolParameters
- options:
The options to apply when activating the session.
- handler:
The callback the system invokes when the operation completes.
Discussion
Configure the session before activating it: call setCategory(_:mode:policy:options:) to set the category and route sharing policy you need.
This method begins activating the audio session asynchronously. The system calls the completion handler as soon as the session has successfully activated or if the activation fails.
Activate playback on watchOS
On watchOS, activating a session with the playback category and the AVAudioSession.RouteSharingPolicy.longFormAudio or AVAudioSession.RouteSharingPolicy.longFormVideo route-sharing policy requires a Bluetooth output route. On supported Apple Watch models running watchOS 11.0 or later, the built-in speaker also satisfies this routing requirement.
The system selects a reachable route automatically when one is available. If no Bluetooth route is available, or when supported AirPods or Beats headphones are nearby:
If the watch supports speaker playback, the system routes to the watch’s built-in speaker.
If the watch does not support speaker playback, the system presents the route picker. If the user dismisses the picker without selecting a valid route, the system calls the completion handler with false.
The system only presents the audio route picker for the playback category and longForm route sharing policy. Use the activate(options:completionHandler:) method instead of setActive(_:options:) to authorize other categories and sharing policies.