如何通过 JavaScript 控制麦克风输出?

发布于 2025-01-13 08:01:25 字数 1358 浏览 0 评论 0原文

我正在构建一个应用程序,它需要录制麦克风。我希望用户控制应用程序的麦克风输出,将其静音/取消静音。

是否可以 ?如果是,我怎样才能实现这一目标?

项目代码的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身骄傲 2025-01-20 08:01:25

您可以通过更改流轨道 muted 属性来将录音静音和取消静音,如下所示:

let tracks = stream.getAudioTracks();

//muting
tracks.forEach(track => {
  track.muted = true;
});

//unmuting
tracks.forEach(track => {
  track.muted = false;
});

您可以在此处找到有关 muted 属性的更多详细说明:
https://developer.mozilla.org/en-US/ docs/Web/API/MediaStreamTrack/muted

如果您需要跟踪 muted 属性,您可以监听 track.onmutetrack.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:

let tracks = stream.getAudioTracks();

//muting
tracks.forEach(track => {
  track.muted = true;
});

//unmuting
tracks.forEach(track => {
  track.muted = false;
});

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 and track.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文