Implemented the resumed interrupted recordings and allowed processing discards on the RecordingViewModel view model in the Recording package target.

This commit is contained in:
2026-07-05 16:16:53 +02:00
parent 8576d6bac6
commit c2ac476257
14 changed files with 563 additions and 309 deletions
@@ -74,18 +74,19 @@ public final class AssetPreinstalling: Preinstalling {
}
/// Preinstalls the speech model assets for the supported equivalent of the given locale, emitting the started, cancelled, and
/// failed downloads through ``events`` a download that finishes, or assets that are already installed, emit nothing.
/// failed downloads through ``events`` a download that finishes, or assets that are already installed, emit nothing. The
/// emitted events carry the given locale, not its supported equivalent, so a caller can match them against its requests.
///
/// - Parameter locale: The locale to preinstall the speech model assets for.
public func preinstall(
for locale: Locale
) async {
guard let locale = await supportedLocale(equivalentTo: locale) else {
guard let supported = await supportedLocale(equivalentTo: locale) else {
return
}
do {
try await install(for: locale) { [continuation] in
try await install(for: supported) { [continuation] in
continuation.yield(.started(locale))
}
} catch {
@@ -94,7 +95,7 @@ public final class AssetPreinstalling: Preinstalling {
} else {
continuation.yield(.failed(locale))
logger.error("The speech model assets for the \"\(locale.identifier, privacy: .public)\" locale failed to preinstall: \(String(describing: error), privacy: .public)")
logger.error("The speech model assets for the \"\(supported.identifier, privacy: .public)\" locale failed to preinstall: \(String(describing: error), privacy: .public)")
}
}
}
@@ -5,7 +5,8 @@ import AVFoundation
/// The service records into a temporary `.m4a` file through an `AVAudioRecorder`, and returns the file's location when the recording stops.
/// The user's permission to record is requested before a recording starts and, on the platforms that require it, the shared audio session is
/// configured for recording while the capture is in progress. When the system interrupts the session while a recording is in progress
/// because of a phone call or Siri, for example the service emits ``CapturingEvent/interrupted`` through ``events``.
/// because of a phone call or Siri, for example the service emits ``CapturingEvent/interrupted`` through ``events``, followed by
/// ``CapturingEvent/interruptionEnded(shouldResume:)`` once the interruption is over.
@MainActor
public final class AudioCapturing: Capturing {
@@ -137,7 +138,7 @@ public final class AudioCapturing: Capturing {
self.recorder = nil
#if os(iOS) || os(visionOS)
try? AVAudioSession.sharedInstance().setActive(false)
try? AVAudioSession.sharedInstance().setActive(false)
#endif
return Constant.File.url
@@ -146,31 +147,60 @@ public final class AudioCapturing: Capturing {
}
#if os(iOS) || os(visionOS)
// MARK: - Helpers
// MARK: - Helpers
private extension AudioCapturing {
private extension AudioCapturing {
/// Handles an interruption notification of the audio session, emitting ``CapturingEvent/interrupted`` through ``events``
/// when the system began interrupting an ongoing recording. The end of an interruption is deliberately ignored: the capture is
/// never resumed without the user asking for it.
///
/// - Parameter notification: The interruption notification posted by the audio session.
func handleInterruption(
_ notification: Notification
) {
guard
recorder != nil,
let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? UInt,
AVAudioSession.InterruptionType(rawValue: typeValue) == .began
else {
return
/// Handles an interruption notification of the audio session while a recording exists, emitting the event the notification
/// describes through ``events``: the beginning of an interruption which pauses the capture emits
/// ``CapturingEvent/interrupted``, and its end emits ``CapturingEvent/interruptionEnded(shouldResume:)`` with the
/// system's hint on whether the capture may resume right away.
///
/// - Parameter notification: The interruption notification posted by the audio session.
func handleInterruption(
_ notification: Notification
) {
guard
recorder != nil,
let event = CapturingEvent(interruption: notification.userInfo)
else {
return
}
continuation.yield(event)
}
continuation.yield(.interrupted)
}
#endif
// MARK: - CapturingEvent+Interruptions
extension CapturingEvent {
/// Creates the event described by the user info of an audio session interruption notification, or `nil` when the user info
/// describes no known interruption.
///
/// - Parameter userInfo: The user info dictionary of the interruption notification.
init?(
interruption userInfo: [AnyHashable: Any]?
) {
guard let typeValue = userInfo?[Constant.Interruption.keyType] as? UInt else {
return nil
}
switch typeValue {
case Constant.Interruption.typeBegan:
self = .interrupted
case Constant.Interruption.typeEnded:
let optionsValue = userInfo?[Constant.Interruption.keyOptions] as? UInt ?? 0
self = .interruptionEnded(shouldResume: optionsValue & Constant.Interruption.optionShouldResume != 0)
default:
return nil
}
}
}
#endif
// MARK: - Constants
@@ -193,4 +223,32 @@ private enum Constant {
/// The location of the temporary file the audio is captured into.
static let url = FileManager.default.temporaryDirectory.appending(path: "recording.m4a")
}
/// The interruption constants, matching AVFoundation's audio session interruption keys and values mirrored as literals on
/// the platforms without an audio session, so the interruption parsing stays compilable, and therefore testable, everywhere.
enum Interruption {
#if os(iOS) || os(visionOS)
/// The user info key of the interruption options.
static let keyOptions = AVAudioSessionInterruptionOptionKey
/// The user info key of the interruption type.
static let keyType = AVAudioSessionInterruptionTypeKey
/// The raw value of the interruption option hinting the capture may resume.
static let optionShouldResume = AVAudioSession.InterruptionOptions.shouldResume.rawValue
/// The raw value of the beginning of an interruption.
static let typeBegan = AVAudioSession.InterruptionType.began.rawValue
/// The raw value of the end of an interruption.
static let typeEnded = AVAudioSession.InterruptionType.ended.rawValue
#else
/// The user info key of the interruption options.
static let keyOptions = "AVAudioSessionInterruptionOptionKey"
/// The user info key of the interruption type.
static let keyType = "AVAudioSessionInterruptionTypeKey"
/// The raw value of the interruption option hinting the capture may resume.
static let optionShouldResume: UInt = 1
/// The raw value of the beginning of an interruption.
static let typeBegan: UInt = 1
/// The raw value of the end of an interruption.
static let typeEnded: UInt = 0
#endif
}
}