@@ -22,25 +22,44 @@ const Record: FC = () => {
22
22
const sourcesRef = useRef < AudioBufferSourceNode [ ] > ( [ ] ) ;
23
23
24
24
useEffect ( ( ) => {
25
- AudioManager . setAudioSessionOptions ( {
26
- iosCategory : 'playAndRecord' ,
27
- iosMode : 'spokenAudio' ,
28
- iosOptions : [ 'defaultToSpeaker' , 'allowBluetoothA2DP' ] ,
29
- } ) ;
30
-
31
25
AudioManager . requestRecordingPermissions ( ) ;
32
-
33
26
recorderRef . current = new AudioRecorder ( {
34
27
sampleRate : SAMPLE_RATE ,
35
28
bufferLengthInSamples : SAMPLE_RATE ,
36
29
} ) ;
30
+ return ( ) => {
31
+ aCtxRef . current ?. close ( ) ;
32
+ } ;
37
33
} , [ ] ) ;
38
34
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
+
39
57
const startEcho = ( ) => {
40
58
if ( ! recorderRef . current ) {
41
59
console . error ( 'AudioContext or AudioRecorder is not initialized' ) ;
42
60
return ;
43
61
}
62
+ setupRecording ( ) ;
44
63
45
64
aCtxRef . current = new AudioContext ( { sampleRate : SAMPLE_RATE } ) ;
46
65
recorderAdapterRef . current = aCtxRef . current . createRecorderAdapter ( ) ;
@@ -58,21 +77,18 @@ const Record: FC = () => {
58
77
59
78
/// This stops only the recording, not the audio context
60
79
const stopEcho = ( ) => {
61
- if ( ! recorderRef . current ) {
62
- console . error ( 'AudioRecorder is not initialized' ) ;
63
- return ;
64
- }
65
- recorderRef . current . stop ( ) ;
80
+ stopRecorder ( ) ;
66
81
aCtxRef . current = null ;
67
82
recorderAdapterRef . current = null ;
68
- console . log ( 'Recording stopped' ) ;
69
83
} ;
70
84
71
85
const startRecordReplay = ( ) => {
72
86
if ( ! recorderRef . current ) {
73
87
console . error ( 'AudioRecorder is not initialized' ) ;
74
88
return ;
75
89
}
90
+ setupRecording ( ) ;
91
+ audioBuffersRef . current = [ ] ;
76
92
77
93
recorderRef . current . onAudioReady ( ( event ) => {
78
94
const { buffer, numFrames, when } = event ;
@@ -89,8 +105,7 @@ const Record: FC = () => {
89
105
recorderRef . current . start ( ) ;
90
106
91
107
setTimeout ( ( ) => {
92
- recorderRef . current ?. stop ( ) ;
93
- console . log ( 'Recording stopped' ) ;
108
+ stopRecorder ( ) ;
94
109
} , 5000 ) ;
95
110
} ;
96
111
0 commit comments