Skip to content

Commit

Permalink
Merge pull request #7047 from vector-im/phlpro/fix_audiosession_vb_re…
Browse files Browse the repository at this point in the history
…corder

Fix AVAudioSession for Voice Broadcast Recording
  • Loading branch information
Phl-Pro authored Nov 9, 2022
2 parents ceebe87 + 75f234d commit 795f382
Showing 1 changed file with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,28 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
// MARK: - VoiceBroadcastRecorderServiceProtocol

func startRecordingVoiceBroadcast() {
let inputNode = audioEngine.inputNode
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)

let inputNode = audioEngine.inputNode

let inputFormat = inputNode.inputFormat(forBus: audioNodeBus)
MXLog.debug("[VoiceBroadcastRecorderService] Start recording voice broadcast for bus name : \(String(describing: inputNode.name(forInputBus: audioNodeBus)))")
let inputFormat = inputNode.inputFormat(forBus: audioNodeBus)
MXLog.debug("[VoiceBroadcastRecorderService] Start recording voice broadcast for bus name : \(String(describing: inputNode.name(forInputBus: audioNodeBus)))")

inputNode.installTap(onBus: audioNodeBus,
bufferSize: 512,
format: inputFormat) { (buffer, time) -> Void in
DispatchQueue.main.async {
self.writeBuffer(buffer)
inputNode.installTap(onBus: audioNodeBus,
bufferSize: 512,
format: inputFormat) { (buffer, time) -> Void in
DispatchQueue.main.async {
self.writeBuffer(buffer)
}
}
}

try? audioEngine.start()
try audioEngine.start()
} catch {
MXLog.debug("[VoiceBroadcastRecorderService] startRecordingVoiceBroadcast error", context: error)
stopRecordingVoiceBroadcast()
}
}

func stopRecordingVoiceBroadcast() {
Expand Down Expand Up @@ -135,6 +143,12 @@ class VoiceBroadcastRecorderService: VoiceBroadcastRecorderServiceProtocol {
private func tearDownVoiceBroadcastService() {
resetValues()
session.tearDownVoiceBroadcastService()

do {
try AVAudioSession.sharedInstance().setActive(false)
} catch {
MXLog.error("[VoiceBroadcastRecorderService] tearDownVoiceBroadcastService error", context: error)
}
}

/// Write audio buffer to chunk file.
Expand Down

0 comments on commit 795f382

Please sign in to comment.