Skip to content

Commit

Permalink
Fix: always release the audio session after the recording stops
Browse files Browse the repository at this point in the history
  • Loading branch information
nimau committed Oct 16, 2023
1 parent 57203f7 commit 98abb34
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 19 deletions.
22 changes: 9 additions & 13 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,19 +282,15 @@ class AudioRecorderMock: AudioRecorderProtocol {
}
//MARK: - stopRecording

var stopRecordingReleaseAudioSessionCallsCount = 0
var stopRecordingReleaseAudioSessionCalled: Bool {
return stopRecordingReleaseAudioSessionCallsCount > 0
}
var stopRecordingReleaseAudioSessionReceivedReleaseAudioSession: Bool?
var stopRecordingReleaseAudioSessionReceivedInvocations: [Bool] = []
var stopRecordingReleaseAudioSessionClosure: ((Bool) -> Void)?

func stopRecording(releaseAudioSession: Bool) {
stopRecordingReleaseAudioSessionCallsCount += 1
stopRecordingReleaseAudioSessionReceivedReleaseAudioSession = releaseAudioSession
stopRecordingReleaseAudioSessionReceivedInvocations.append(releaseAudioSession)
stopRecordingReleaseAudioSessionClosure?(releaseAudioSession)
var stopRecordingCallsCount = 0
var stopRecordingCalled: Bool {
return stopRecordingCallsCount > 0
}
var stopRecordingClosure: (() -> Void)?

func stopRecording() {
stopRecordingCallsCount += 1
stopRecordingClosure?()
}
//MARK: - averagePowerForChannelNumber

Expand Down
4 changes: 1 addition & 3 deletions ElementX/Sources/Services/Audio/Recorder/AudioRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ class AudioRecorder: NSObject, AudioRecorderProtocol, AVAudioRecorderDelegate {
}
}

func stopRecording(releaseAudioSession: Bool = true) {
func stopRecording() {
audioRecorder?.stop()

// Release audio session if requested
guard releaseAudioSession else { return }
do {
try AVAudioSession.sharedInstance().setActive(false)
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protocol AudioRecorderProtocol: AnyObject {
var url: URL? { get }

func recordWithOutputURL(_ url: URL)
func stopRecording(releaseAudioSession: Bool)
func stopRecording()
func averagePowerForChannelNumber(_ channelNumber: Int) -> Float
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AudioRecorderState: ObservableObject, Identifiable {

func detachAudioRecorder() {
guard audioRecorder != nil else { return }
audioRecorder?.stopRecording(releaseAudioSession: true)
audioRecorder?.stopRecording()
stopPublishUpdates()
cancellables = []
audioRecorder = nil
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Sources/AudioRecorderStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AudioRecorderStateTests: XCTestCase {
audioRecorderState.attachAudioRecorder(audioRecorderMock)

audioRecorderState.detachAudioRecorder()
XCTAssert(audioRecorderMock.stopRecordingReleaseAudioSessionCalled)
XCTAssert(audioRecorderMock.stopRecordingCalled)
XCTAssertEqual(audioRecorderState.recordingState, .stopped)
}

Expand Down

0 comments on commit 98abb34

Please sign in to comment.