Skip to content

Commit e43d8fa

Browse files
committed
[Fix]AudioRecorder state after nullifying ADM
1 parent ac4c5e8 commit e43d8fa

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

Sources/StreamVideo/Utils/AudioSession/AudioRecorder/Namespace/Middleware/StreamCallAudioRecorder+AVAudioRecorderMiddleware.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,7 @@ extension StreamCallAudioRecorder.Namespace {
6161
audioDeviceModuleCancellable = audioStore
6262
.publisher(\.audioDeviceModule)
6363
.receive(on: processingQueue)
64-
.sink { [weak self] in
65-
if self?.updateMetersCancellable != nil {
66-
self?.stopRecording()
67-
self?.startRecording()
68-
}
69-
70-
// We restore the mode to whatever we had before the call.
71-
if let audioDeviceModule = $0 {
72-
self?.mode = .audioDeviceModule(audioDeviceModule)
73-
} else {
74-
self?.mode = initialMode
75-
}
76-
}
64+
.sink { [weak self] in self?.didUpdate($0, initialMode: initialMode) }
7765
}
7866

7967
// MARK: - Middleware
@@ -243,5 +231,27 @@ extension StreamCallAudioRecorder.Namespace {
243231
log.debug("AVAudioDeviceModule audioLevel observation started...", subsystems: .audioRecording)
244232
}
245233
}
234+
235+
private func didUpdate(
236+
_ audioDeviceModule: AudioDeviceModule?,
237+
initialMode: Mode
238+
) {
239+
stopRecording()
240+
241+
let newMode: Mode = {
242+
if let audioDeviceModule {
243+
return .audioDeviceModule(audioDeviceModule)
244+
} else {
245+
return initialMode
246+
}
247+
}()
248+
249+
processingQueue.addTaskOperation { [weak self] in
250+
self?.mode = newMode
251+
if self?.state?.shouldRecord == true, self?.state?.isRecording == true {
252+
self?.startRecording()
253+
}
254+
}
255+
}
246256
}
247257
}

0 commit comments

Comments
 (0)