getdisplaymedia不会录制我的电脑的所有音频输出

k10s72fa  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(448)

我很难用getdisplaymedia录制我的计算机屏幕及其所有音频接口(系统+麦克风),但是我使用的代码来自引用此代码的react js组件

const defaultDisplayMediaOptions = {
  video: {
    cursor: "never",
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100,
  },
};

export default function useScreenRecording({
  displayMediaOptions = defaultDisplayMediaOptions,
  onEnd = () => {},
  onError = () => {},
  onStart = () => {},
} = {}) {
  const [captureStream, setCaptureStream] = React.useState(null);
  const [error, setError] = React.useState(null);
  const [mediaRecorder, setMediaRecorder] = React.useState(null);
  const [isRecording, setIsRecording] = React.useState(false);
  const [recording, setRecording] = React.useState(null);

  const stopRecording = () => {
    try {
      setIsRecording(false);
      mediaRecorder.stop();
      captureStream.getTracks().forEach(track => track.stop());
    } catch (e) {
      onError(e);
      setError(e);
    }
  };
  const startRecording = async () => {
    try {
      const stream = await navigator.mediaDevices.getDisplayMedia(
        displayMediaOptions,
      );

      setIsRecording(true);
      stream.getTracks().forEach(track => {
        track.onended = stopRecording;
      });

      setCaptureStream(stream);
      const recorder = new MediaRecorder(stream);
      recorder.ondataavailable = event => {
        onEnd(event);
        setRecording(event.data);
      };
      recorder.start();
      setMediaRecorder(recorder);
      onStart({ stream, recorder });
    } catch (e) {
      setIsRecording(false);
      onError(e);
      setError(e);
    }
  };

到目前为止,这段录音只能获得我电脑的屏幕和系统的音频。有人能帮我回答这个问题吗。可以录制我的电脑屏幕和所有音频输出。我用getusermedia做了一个测试录制,它的音频捕获效果和我预期的一样100%,但displaymedia没有这样做。提前感谢您的帮助

fnx2tebb

fnx2tebb1#

您可以将曲目从getusermedia合并到displaymedia,以从用户麦克风获取音频。

const defaultDisplayMediaOptions = {
  video: {
    cursor: "never",
  },
  audio: {
    echoCancellation: true,
    noiseSuppression: true,
    sampleRate: 44100,
  },
};

export default function useScreenRecording({
  displayMediaOptions = defaultDisplayMediaOptions,
  onEnd = () => {},
  onError = () => {},
  onStart = () => {},
} = {}) {
  const [captureStream, setCaptureStream] = React.useState(null);
  const [error, setError] = React.useState(null);
  const [mediaRecorder, setMediaRecorder] = React.useState(null);
  const [isRecording, setIsRecording] = React.useState(false);
  const [recording, setRecording] = React.useState(null);

  const stopRecording = () => {
    try {
      setIsRecording(false);
      mediaRecorder.stop();
      captureStream.getTracks().forEach(track => track.stop());
    } catch (e) {
      onError(e);
      setError(e);
    }
  };
  const startRecording = async () => {
    try {
      const stream = await navigator.mediaDevices.getDisplayMedia(
        displayMediaOptions,
      );
      const audioStream = await navigator.mediaDevices.getUserMedia({audio:true})

      setIsRecording(true);
      stream.getTracks().forEach(track => {
        track.onended = stopRecording;
      });
      audioStream.getAudioTracks().forEach(track=>{
          stream.addTrack(track);
      });

      setCaptureStream(stream);
      const recorder = new MediaRecorder(stream);
      recorder.ondataavailable = event => {
        onEnd(event);
        setRecording(event.data);
      };
      recorder.start();
      setMediaRecorder(recorder);
      onStart({ stream, recorder });
    } catch (e) {
      setIsRecording(false);
      onError(e);
      setError(e);
    }
  };

相关问题