'How to record a audio output using MediaStream API?

I am developing an application where I would like to record audio output of a user's computer. Say for example, If user is watching any video (in player or youtube or netflix) in his computer, I want to record the sound.

Basically, I want to record stereo mix.

Is there any way to acheive that using Javascript ?



Solution 1:[1]

If you are using electron then you can use desktop capturer :

// In the renderer process.
const { desktopCapturer } = require('electron')

desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
  for (const source of sources) {
    if (source.name === 'Electron') {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({
          audio: false,
          video: {
            mandatory: {
              chromeMediaSource: 'desktop',
              chromeMediaSourceId: source.id,
              minWidth: 1280,
              maxWidth: 1280,
              minHeight: 720,
              maxHeight: 720
            }
          }
        })
        handleStream(stream)
      } catch (e) {
        handleError(e)
      }
      return
    }
  }
})

function handleStream (stream) {
  const recorder = new MediaRecoder(stream);
  // your code here
}

function handleError (e) {
  console.log(e)
}

Read more about it here : https://www.electronjs.org/docs/latest/api/desktop-capturer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Kumar