@intentai/react 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +23 -0
- package/dist/index.mjs +23 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -116,15 +116,32 @@ var useAudioLevel = (isRecording) => {
|
|
|
116
116
|
const [audioLevel, setAudioLevel] = (0, import_react.useState)(0);
|
|
117
117
|
const analyserRef = (0, import_react.useRef)(null);
|
|
118
118
|
const animationRef = (0, import_react.useRef)();
|
|
119
|
+
const streamRef = (0, import_react.useRef)(null);
|
|
120
|
+
const audioContextRef = (0, import_react.useRef)(null);
|
|
119
121
|
(0, import_react.useEffect)(() => {
|
|
120
122
|
if (!isRecording) {
|
|
121
123
|
setAudioLevel(0);
|
|
124
|
+
if (animationRef.current) {
|
|
125
|
+
cancelAnimationFrame(animationRef.current);
|
|
126
|
+
animationRef.current = void 0;
|
|
127
|
+
}
|
|
128
|
+
if (streamRef.current) {
|
|
129
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
130
|
+
streamRef.current = null;
|
|
131
|
+
}
|
|
132
|
+
if (audioContextRef.current) {
|
|
133
|
+
audioContextRef.current.close();
|
|
134
|
+
audioContextRef.current = null;
|
|
135
|
+
}
|
|
136
|
+
analyserRef.current = null;
|
|
122
137
|
return;
|
|
123
138
|
}
|
|
124
139
|
const initAudio = async () => {
|
|
125
140
|
try {
|
|
126
141
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
142
|
+
streamRef.current = stream;
|
|
127
143
|
const audioContext = new AudioContext();
|
|
144
|
+
audioContextRef.current = audioContext;
|
|
128
145
|
const source = audioContext.createMediaStreamSource(stream);
|
|
129
146
|
const analyser = audioContext.createAnalyser();
|
|
130
147
|
analyser.fftSize = 256;
|
|
@@ -148,6 +165,12 @@ var useAudioLevel = (isRecording) => {
|
|
|
148
165
|
if (animationRef.current) {
|
|
149
166
|
cancelAnimationFrame(animationRef.current);
|
|
150
167
|
}
|
|
168
|
+
if (streamRef.current) {
|
|
169
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
170
|
+
}
|
|
171
|
+
if (audioContextRef.current) {
|
|
172
|
+
audioContextRef.current.close();
|
|
173
|
+
}
|
|
151
174
|
};
|
|
152
175
|
}, [isRecording]);
|
|
153
176
|
return audioLevel;
|
package/dist/index.mjs
CHANGED
|
@@ -98,15 +98,32 @@ var useAudioLevel = (isRecording) => {
|
|
|
98
98
|
const [audioLevel, setAudioLevel] = useState(0);
|
|
99
99
|
const analyserRef = useRef(null);
|
|
100
100
|
const animationRef = useRef();
|
|
101
|
+
const streamRef = useRef(null);
|
|
102
|
+
const audioContextRef = useRef(null);
|
|
101
103
|
useEffect(() => {
|
|
102
104
|
if (!isRecording) {
|
|
103
105
|
setAudioLevel(0);
|
|
106
|
+
if (animationRef.current) {
|
|
107
|
+
cancelAnimationFrame(animationRef.current);
|
|
108
|
+
animationRef.current = void 0;
|
|
109
|
+
}
|
|
110
|
+
if (streamRef.current) {
|
|
111
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
112
|
+
streamRef.current = null;
|
|
113
|
+
}
|
|
114
|
+
if (audioContextRef.current) {
|
|
115
|
+
audioContextRef.current.close();
|
|
116
|
+
audioContextRef.current = null;
|
|
117
|
+
}
|
|
118
|
+
analyserRef.current = null;
|
|
104
119
|
return;
|
|
105
120
|
}
|
|
106
121
|
const initAudio = async () => {
|
|
107
122
|
try {
|
|
108
123
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
124
|
+
streamRef.current = stream;
|
|
109
125
|
const audioContext = new AudioContext();
|
|
126
|
+
audioContextRef.current = audioContext;
|
|
110
127
|
const source = audioContext.createMediaStreamSource(stream);
|
|
111
128
|
const analyser = audioContext.createAnalyser();
|
|
112
129
|
analyser.fftSize = 256;
|
|
@@ -130,6 +147,12 @@ var useAudioLevel = (isRecording) => {
|
|
|
130
147
|
if (animationRef.current) {
|
|
131
148
|
cancelAnimationFrame(animationRef.current);
|
|
132
149
|
}
|
|
150
|
+
if (streamRef.current) {
|
|
151
|
+
streamRef.current.getTracks().forEach((track) => track.stop());
|
|
152
|
+
}
|
|
153
|
+
if (audioContextRef.current) {
|
|
154
|
+
audioContextRef.current.close();
|
|
155
|
+
}
|
|
133
156
|
};
|
|
134
157
|
}, [isRecording]);
|
|
135
158
|
return audioLevel;
|
package/package.json
CHANGED