@@ -23,7 +23,6 @@ function useVoiceVisualizer({
23
23
} : useVoiceVisualizerParams = { } ) : Controls {
24
24
const [ isRecordingInProgress , setIsRecordingInProgress ] = useState ( false ) ;
25
25
const [ isPausedRecording , setIsPausedRecording ] = useState ( false ) ;
26
- const [ audioStream , setAudioStream ] = useState < MediaStream | null > ( null ) ;
27
26
const [ audioData , setAudioData ] = useState < Uint8Array > ( new Uint8Array ( 0 ) ) ;
28
27
const [ isProcessingAudioOnComplete , _setIsProcessingAudioOnComplete ] =
29
28
useState ( false ) ;
@@ -43,6 +42,7 @@ function useVoiceVisualizer({
43
42
const [ isProcessingStartRecording , setIsProcessingStartRecording ] =
44
43
useState ( false ) ;
45
44
45
+ const audioStreamRef = useRef < MediaStream | null > ( null ) ;
46
46
const mediaRecorderRef = useRef < MediaRecorder | null > ( null ) ;
47
47
const audioContextRef = useRef < AudioContext | null > ( null ) ;
48
48
const analyserRef = useRef < AnalyserNode | null > ( null ) ;
@@ -160,7 +160,7 @@ function useVoiceVisualizer({
160
160
setIsProcessingStartRecording ( false ) ;
161
161
setIsRecordingInProgress ( true ) ;
162
162
setPrevTime ( performance . now ( ) ) ;
163
- setAudioStream ( stream ) ;
163
+ audioStreamRef . current = stream ;
164
164
audioContextRef . current = new window . AudioContext ( ) ;
165
165
analyserRef . current = audioContextRef . current . createAnalyser ( ) ;
166
166
dataArrayRef . current = new Uint8Array (
@@ -228,7 +228,7 @@ function useVoiceVisualizer({
228
228
handleDataAvailable
229
229
) ;
230
230
}
231
- audioStream ?. getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
231
+ audioStreamRef . current ?. getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
232
232
if ( rafRecordingRef . current ) cancelAnimationFrame ( rafRecordingRef . current ) ;
233
233
if ( sourceRef . current ) sourceRef . current . disconnect ( ) ;
234
234
if ( audioContextRef . current && audioContextRef . current . state !== "closed" ) {
@@ -258,7 +258,8 @@ function useVoiceVisualizer({
258
258
mediaRecorderRef . current = null ;
259
259
}
260
260
261
- audioStream ?. getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
261
+ audioStreamRef . current ?. getTracks ( ) . forEach ( ( track ) => track . stop ( ) ) ;
262
+ audioStreamRef . current = null ;
262
263
if ( audioRef ?. current ) {
263
264
audioRef . current . removeEventListener ( "ended" , onEndedRecordedAudio ) ;
264
265
audioRef . current . pause ( ) ;
@@ -270,7 +271,6 @@ function useVoiceVisualizer({
270
271
dataArrayRef . current = null ;
271
272
sourceRef . current = null ;
272
273
273
- setAudioStream ( null ) ;
274
274
setIsProcessingStartRecording ( false ) ;
275
275
setIsRecordingInProgress ( false ) ;
276
276
setIsPreloadedBlob ( false ) ;
0 commit comments