5
5
AudioRecorder ,
6
6
RecorderAdapterNode ,
7
7
AudioBufferSourceNode ,
8
- AudioBuffer
8
+ AudioBuffer ,
9
9
} from 'react-native-audio-api' ;
10
10
11
11
import { Container , Button } from '../../components' ;
@@ -21,21 +21,21 @@ const Record: FC = () => {
21
21
const audioBuffersRef = useRef < AudioBuffer [ ] > ( [ ] ) ;
22
22
const sourcesRef = useRef < AudioBufferSourceNode [ ] > ( [ ] ) ;
23
23
24
-
25
24
useEffect ( ( ) => {
26
25
AudioManager . setAudioSessionOptions ( {
27
26
iosCategory : 'playAndRecord' ,
28
27
iosMode : 'spokenAudio' ,
29
28
iosOptions : [ 'defaultToSpeaker' , 'allowBluetoothA2DP' ] ,
30
29
} ) ;
31
-
30
+
31
+ AudioManager . requestRecordingPermissions ( ) ;
32
+
32
33
recorderRef . current = new AudioRecorder ( {
33
34
sampleRate : SAMPLE_RATE ,
34
35
bufferLengthInSamples : SAMPLE_RATE ,
35
36
} ) ;
36
37
} , [ ] ) ;
37
38
38
-
39
39
const startEcho = ( ) => {
40
40
if ( ! recorderRef . current ) {
41
41
console . error ( 'AudioContext or AudioRecorder is not initialized' ) ;
@@ -46,15 +46,15 @@ const Record: FC = () => {
46
46
recorderAdapterRef . current = aCtxRef . current . createRecorderAdapter ( ) ;
47
47
recorderAdapterRef . current . connect ( aCtxRef . current . destination ) ;
48
48
recorderRef . current . connect ( recorderAdapterRef . current ) ;
49
-
49
+
50
50
recorderRef . current . start ( ) ;
51
- console . log ( 'Recording started' ) ;
51
+ console . log ( 'Recording started' ) ;
52
52
console . log ( 'Audio context state:' , aCtxRef . current . state ) ;
53
53
if ( aCtxRef . current . state === 'suspended' ) {
54
54
console . log ( 'Resuming audio context' ) ;
55
55
aCtxRef . current . resume ( ) ;
56
56
}
57
- }
57
+ } ;
58
58
59
59
/// This stops only the recording, not the audio context
60
60
const stopEcho = ( ) => {
@@ -66,7 +66,7 @@ const Record: FC = () => {
66
66
aCtxRef . current = null ;
67
67
recorderAdapterRef . current = null ;
68
68
console . log ( 'Recording stopped' ) ;
69
- }
69
+ } ;
70
70
71
71
const startRecordReplay = ( ) => {
72
72
if ( ! recorderRef . current ) {
@@ -92,8 +92,7 @@ const Record: FC = () => {
92
92
recorderRef . current ?. stop ( ) ;
93
93
console . log ( 'Recording stopped' ) ;
94
94
} , 5000 ) ;
95
-
96
- }
95
+ } ;
97
96
98
97
const stopRecordReplay = ( ) => {
99
98
const aCtx = new AudioContext ( { sampleRate : SAMPLE_RATE } ) ;
@@ -128,19 +127,22 @@ const Record: FC = () => {
128
127
} ,
129
128
( nextStartAt - tNow ) * 1000
130
129
) ;
131
-
132
- }
130
+ } ;
133
131
134
132
return (
135
133
< Container style = { { gap : 40 } } >
136
- < Text style = { { color : colors . white , fontSize : 24 , textAlign : 'center' } } > Sample rate: { SAMPLE_RATE } </ Text >
134
+ < Text style = { { color : colors . white , fontSize : 24 , textAlign : 'center' } } >
135
+ Sample rate: { SAMPLE_RATE }
136
+ </ Text >
137
137
< View style = { { alignItems : 'center' , justifyContent : 'center' , gap : 5 } } >
138
138
< Text style = { { color : colors . white , fontSize : 24 } } > Echo example</ Text >
139
139
< Button title = "Start Recording" onPress = { startEcho } />
140
140
< Button title = "Stop Recording" onPress = { stopEcho } />
141
141
</ View >
142
142
< View style = { { alignItems : 'center' , justifyContent : 'center' , gap : 5 } } >
143
- < Text style = { { color : colors . white , fontSize : 24 } } > Record & replay example </ Text >
143
+ < Text style = { { color : colors . white , fontSize : 24 } } >
144
+ Record & replay example
145
+ </ Text >
144
146
< Button title = "Record for Replay" onPress = { startRecordReplay } />
145
147
< Button title = "Replay" onPress = { stopRecordReplay } />
146
148
</ View >
0 commit comments