Improved the Capturing protocol in the Recording package target to capture interruptions and pause any active capture process.
This commit is contained in:
@@ -47,6 +47,10 @@ extension RecordingView {
|
||||
@ObservationIgnored
|
||||
private var taskCapturer: Task<Void, Never>?
|
||||
|
||||
/// The task that listens to the events emitted by the capturing service, or `nil` until the first recording starts.
|
||||
@ObservationIgnored
|
||||
private var taskEvents: Task<Void, Never>?
|
||||
|
||||
/// The task that updates ``elapsedSeconds`` from the measured recording time once per second while recording.
|
||||
@ObservationIgnored
|
||||
private var taskTimer: Task<Void, Never>?
|
||||
@@ -130,16 +134,11 @@ extension RecordingView {
|
||||
case .notRecording:
|
||||
state = .recording
|
||||
|
||||
listenToEvents()
|
||||
startTimer(true)
|
||||
startCapturer(true)
|
||||
case .recording:
|
||||
state = .paused
|
||||
|
||||
stopTimer()
|
||||
|
||||
enqueueCapturer {
|
||||
try? await self.capturer.pause()
|
||||
}
|
||||
pauseRecording()
|
||||
case .paused:
|
||||
state = .recording
|
||||
|
||||
@@ -224,6 +223,40 @@ private extension RecordingView.Model {
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts listening to the events emitted by the capturing service, unless already listening: an interruption of the capture pauses
|
||||
/// an ongoing recording, exactly like a press of the main button would.
|
||||
func listenToEvents() {
|
||||
guard taskEvents == nil else {
|
||||
return
|
||||
}
|
||||
|
||||
taskEvents = Task { [weak self, events = capturer.events] in
|
||||
for await event in events {
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
|
||||
switch event {
|
||||
case .interrupted:
|
||||
if state == .recording {
|
||||
pauseRecording()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pauses the ongoing recording: it stops the timer and pauses the audio capture.
|
||||
func pauseRecording() {
|
||||
state = .paused
|
||||
|
||||
stopTimer()
|
||||
|
||||
enqueueCapturer {
|
||||
try? await self.capturer.pause()
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts the audio capture through the attached ``Capturing``, falling back to the not-recording state when the service fails.
|
||||
///
|
||||
/// - Parameter isNewRecording: Whether a new recording should be started, as opposed to a paused one being resumed.
|
||||
|
||||
Reference in New Issue
Block a user