Skip to content

Commit

Permalink
fix: added graceful route change event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
demchuk-alex committed Oct 23, 2024
1 parent 0de133b commit 6d5c284
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
30 changes: 19 additions & 11 deletions ios/AudioSessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`);
}
}
Expand Down

0 comments on commit 6d5c284

Please sign in to comment.