如何通过 JavaScript 控制麦克风输出?
我正在构建一个应用程序,它需要录制麦克风。我希望用户控制应用程序的麦克风输出,将其静音/取消静音。
是否可以 ?如果是,我怎样才能实现这一目标?
项目代码的 React 版本
"react": "^17.0.2",
我获得麦克风输入的
const handleRecord = async () => {
setIsAudioLoading(true);
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/wav" });
let orderNumber = 0;
let chunks = [];
mediaRecorder.ondataavailable = function (e) {
chunks.push(e.data);
};
mediaRecorder.onstop = function () {
const blob = new Blob(chunks, { type: "audio/wav" });
streamChunk(blob, orderNumber, "mic");
chunks = [];
};
mediaRecorder.start();
setStopMic(() => () => {
setIsMicPaused(true);
setCurrentAudioTime(lastChunkSecondRef.current - 1.5);
isMicPausedRef.current = true;
mediaRecorder.stop();
});
setStartMic(() => () => {
setIsMicPaused(false);
isMicPausedRef.current = false;
mediaRecorder.start();
});
setInterval(function () {
if (!isMicPausedRef.current) {
orderNumber++;
mediaRecorder.stop();
mediaRecorder.start();
}
}, MIC_INTERVAL);
};
I am building an app, which needs to record a microphone. I want users to control microphone output from the app, mute/unmute it.
Is it possible ? If yes, how can I achieve that ?
React version of the project
"react": "^17.0.2",
Code where I get an microphono input
const handleRecord = async () => {
setIsAudioLoading(true);
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream, { mimeType: "audio/wav" });
let orderNumber = 0;
let chunks = [];
mediaRecorder.ondataavailable = function (e) {
chunks.push(e.data);
};
mediaRecorder.onstop = function () {
const blob = new Blob(chunks, { type: "audio/wav" });
streamChunk(blob, orderNumber, "mic");
chunks = [];
};
mediaRecorder.start();
setStopMic(() => () => {
setIsMicPaused(true);
setCurrentAudioTime(lastChunkSecondRef.current - 1.5);
isMicPausedRef.current = true;
mediaRecorder.stop();
});
setStartMic(() => () => {
setIsMicPaused(false);
isMicPausedRef.current = false;
mediaRecorder.start();
});
setInterval(function () {
if (!isMicPausedRef.current) {
orderNumber++;
mediaRecorder.stop();
mediaRecorder.start();
}
}, MIC_INTERVAL);
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过更改流轨道
muted
属性来将录音静音和取消静音,如下所示:您可以在此处找到有关 muted 属性的更多详细说明:
https://developer.mozilla.org/en-US/ docs/Web/API/MediaStreamTrack/muted
如果您需要跟踪 muted 属性,您可以监听
track.onmute
和track.onunmute
事件。如果您还需要调整录音的音量,您应该查看一下 Web Audio API:
https://developer.mozilla.org/en-US/docs/网络/API/Web_Audio_API
You can mute and unmute your recording by changing the stream track
muted
property like so:You can find more a detailed description of the muted property here:
https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack/muted
In case you need to track the muted property, you can listen to
track.onmute
andtrack.onunmute
events.If you need to adjust the volume of your recording aswell, you should take a look into the Web Audio API:
https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API