Skip to content

Commit b880d55

Browse files
authored
fix: proper recording stopping (#646)
* fix: proper recording stopping * refactor: brought back 5 seconds of recording * fix: removed detaching from stopping player * fix: added closing context after leaving
1 parent 125696f commit b880d55

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

apps/common-app/src/examples/Record/Record.tsx

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,44 @@ const Record: FC = () => {
2222
const sourcesRef = useRef<AudioBufferSourceNode[]>([]);
2323

2424
useEffect(() => {
25-
AudioManager.setAudioSessionOptions({
26-
iosCategory: 'playAndRecord',
27-
iosMode: 'spokenAudio',
28-
iosOptions: ['defaultToSpeaker', 'allowBluetoothA2DP'],
29-
});
30-
3125
AudioManager.requestRecordingPermissions();
32-
3326
recorderRef.current = new AudioRecorder({
3427
sampleRate: SAMPLE_RATE,
3528
bufferLengthInSamples: SAMPLE_RATE,
3629
});
30+
return () => {
31+
aCtxRef.current?.close();
32+
};
3733
}, []);
3834

35+
const setupRecording = () => {
36+
AudioManager.setAudioSessionOptions({
37+
iosCategory: 'playAndRecord',
38+
iosMode: 'spokenAudio',
39+
iosOptions: ['defaultToSpeaker', 'allowBluetoothA2DP'],
40+
});
41+
};
42+
43+
const stopRecorder = () => {
44+
if (recorderRef.current) {
45+
recorderRef.current.stop();
46+
console.log('Recording stopped');
47+
// advised, but not required
48+
AudioManager.setAudioSessionOptions({
49+
iosCategory: 'playback',
50+
iosMode: 'default',
51+
});
52+
} else {
53+
console.error('AudioRecorder is not initialized');
54+
}
55+
};
56+
3957
const startEcho = () => {
4058
if (!recorderRef.current) {
4159
console.error('AudioContext or AudioRecorder is not initialized');
4260
return;
4361
}
62+
setupRecording();
4463

4564
aCtxRef.current = new AudioContext({ sampleRate: SAMPLE_RATE });
4665
recorderAdapterRef.current = aCtxRef.current.createRecorderAdapter();
@@ -58,21 +77,18 @@ const Record: FC = () => {
5877

5978
/// This stops only the recording, not the audio context
6079
const stopEcho = () => {
61-
if (!recorderRef.current) {
62-
console.error('AudioRecorder is not initialized');
63-
return;
64-
}
65-
recorderRef.current.stop();
80+
stopRecorder();
6681
aCtxRef.current = null;
6782
recorderAdapterRef.current = null;
68-
console.log('Recording stopped');
6983
};
7084

7185
const startRecordReplay = () => {
7286
if (!recorderRef.current) {
7387
console.error('AudioRecorder is not initialized');
7488
return;
7589
}
90+
setupRecording();
91+
audioBuffersRef.current = [];
7692

7793
recorderRef.current.onAudioReady((event) => {
7894
const { buffer, numFrames, when } = event;
@@ -89,8 +105,7 @@ const Record: FC = () => {
89105
recorderRef.current.start();
90106

91107
setTimeout(() => {
92-
recorderRef.current?.stop();
93-
console.log('Recording stopped');
108+
stopRecorder();
94109
}, 5000);
95110
};
96111

packages/react-native-audio-api/ios/audioapi/ios/system/AudioEngine.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ - (void)detachInputNode
204204

205205
[self.audioEngine detachNode:self.inputNode];
206206
self.inputNode = nil;
207+
[self restartAudioEngine];
207208
}
208209

209210
- (bool)startIfNecessary

0 commit comments

Comments
 (0)