|
5 | 5 | // Created by Brian D Keane on 1/6/25. |
6 | 6 | // |
7 | 7 |
|
8 | | -import AVFoundation |
| 8 | +@preconcurrency import AVFoundation |
9 | 9 | import Foundation |
10 | 10 | import PlayolaCore |
11 | 11 | import os.log |
@@ -138,30 +138,43 @@ open class PlayolaMainMixer: NSObject { |
138 | 138 | } |
139 | 139 |
|
140 | 140 | extension PlayolaMainMixer { |
| 141 | + /// Starts the audio engine off the main thread to avoid blocking on |
| 142 | + /// AUIOClient_StartIO during cold hardware initialization (e.g. resuming |
| 143 | + /// from a phone-call interruption). AVAudioEngine.start() is thread-safe; |
| 144 | + /// only the start call itself is dispatched off main. |
141 | 145 | @MainActor |
142 | | - public func start() throws { |
| 146 | + public func start() async throws { |
| 147 | + let engine = self.engine |
143 | 148 | do { |
144 | | - try engine.start() |
145 | | - } catch { |
146 | | - Task { |
147 | | - await errorReporter.reportError( |
148 | | - error, context: "Failed to start audio engine", |
149 | | - level: .critical) |
| 149 | + try await withCheckedThrowingContinuation { |
| 150 | + (continuation: CheckedContinuation<Void, Error>) in |
| 151 | + DispatchQueue.global(qos: .userInitiated).async { |
| 152 | + do { |
| 153 | + try engine.start() |
| 154 | + continuation.resume() |
| 155 | + } catch { |
| 156 | + continuation.resume(throwing: error) |
| 157 | + } |
| 158 | + } |
150 | 159 | } |
| 160 | + } catch { |
| 161 | + await errorReporter.reportError( |
| 162 | + error, context: "Failed to start audio engine", |
| 163 | + level: .critical) |
151 | 164 | throw error |
152 | 165 | } |
153 | 166 | } |
154 | 167 |
|
155 | 168 | @MainActor |
156 | | - public func restartEngine() throws { |
| 169 | + public func restartEngine() async throws { |
157 | 170 | os_log("Restarting audio engine", log: PlayolaMainMixer.logger, type: .info) |
158 | 171 |
|
159 | 172 | if engine.isRunning { |
160 | 173 | engine.stop() |
161 | 174 | } |
162 | 175 |
|
163 | 176 | engine.prepare() |
164 | | - try start() |
| 177 | + try await start() |
165 | 178 | } |
166 | 179 |
|
167 | 180 | public var isEngineRunning: Bool { |
|
0 commit comments