From 6d5c2841e6b3f7fd5b75d027de8ea029f3b5dece Mon Sep 17 00:00:00 2001 From: Alexander Demchuk Date: Wed, 23 Oct 2024 20:15:09 +0200 Subject: [PATCH] fix: added graceful route change event handling --- ios/AudioSessionManager.swift | 30 +++++++++++++++++++----------- package.json | 2 +- src/index.ts | 13 +++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/ios/AudioSessionManager.swift b/ios/AudioSessionManager.swift index 528fd60..b41a7cc 100644 --- a/ios/AudioSessionManager.swift +++ b/ios/AudioSessionManager.swift @@ -97,12 +97,16 @@ class AudioSessionManager { do { switch reason { case .newDeviceAvailable, .oldDeviceUnavailable: - if self.audioPlayerNode != nil { - Logger.debug("Destroying playback") + if let node = audioPlayerNode, self.audioEngine.isRunning, node.isPlaying { + node.pause() + node.stop() + self.audioEngine.stop() self.destroyPlayerNode() + self.audioEngine = AVAudioEngine() + } else { + self.destroyPlayerNode() + try self.restartAudioSessionForPlayback() } - - try self.restartAudioSessionForPlayback() case .categoryChange: print("Category Changed") default: @@ -636,13 +640,17 @@ class AudioSessionManager { // Update RIFF chunk size at offset 4 fileHandle.seek(toFileOffset: 4) - let fileSizeBytes = UInt32(fileSize).littleEndianBytes - fileHandle.write(Data(fileSizeBytes)) - - // Update data chunk size at offset 40 - fileHandle.seek(toFileOffset: 40) - let dataSizeBytes = UInt32(dataSize).littleEndianBytes - fileHandle.write(Data(dataSizeBytes)) + if (fileSize > 0) { + let fileSizeBytes = UInt32(fileSize).littleEndianBytes + fileHandle.write(Data(fileSizeBytes)) + + // Update data chunk size at offset 40 + fileHandle.seek(toFileOffset: 40) + let dataSizeBytes = UInt32(dataSize).littleEndianBytes + fileHandle.write(Data(dataSizeBytes)) + } else { + Logger.debug("File size is negative which means it is an error before") + } } catch let error { Logger.debug("Error updating WAV header: \(error)") diff --git a/package.json b/package.json index 4d30af3..da633b8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mykin-ai/expo-audio-stream", - "version": "0.2.1", + "version": "0.2.2", "description": "Expo Play Audio Stream module", "main": "build/index.js", "types": "build/index.d.ts", diff --git a/src/index.ts b/src/index.ts index a786511..f6856b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,12 +16,14 @@ export class ExpoPlayAudioStream { recordingConfig: RecordingConfig ): Promise<{ recordingResult: StartRecordingResult; - subscription: Subscription; + subscription?: Subscription; }> { - try { - const { onAudioStream, ...options } = recordingConfig; + const { onAudioStream, ...options } = recordingConfig; + + let subscription: Subscription | undefined; - const subscription = addAudioEventListener( + if (onAudioStream && typeof onAudioStream == 'function') { + subscription = addAudioEventListener( async (event: AudioEventPayload) => { const { fileUri, deltaSize, totalSize, position, encoded } = event; if (!encoded) { @@ -39,13 +41,16 @@ export class ExpoPlayAudioStream { }); } ); + } + try { const recordingResult = await ExpoPlayAudioStreamModule.startRecording( options ); return { recordingResult, subscription }; } catch (error) { console.error(error); + subscription?.remove(); throw new Error(`Failed to start recording: ${error}`); } }