- copy and paste the code to browser console
- run
record();
to record audio output - run
stop();
to stop recording - run
download()
to download last recording
Last active
August 20, 2021 13:34
-
-
Save technikhil314/971c2a99a801f0857243256b2b0672d0 to your computer and use it in GitHub Desktop.
Browser based audio output recorder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let recorder; | |
let recordedChunks = []; | |
function dataAvailable(event) { | |
if (event.data.size > 0) { | |
recordedChunks.push(event.data); | |
} | |
} | |
function resetState() { | |
recorder = null; | |
recordedChunks = []; | |
} | |
function download() { | |
let blob = new Blob(recordedChunks, { | |
type: "audio/aac", | |
}); | |
let url = URL.createObjectURL(blob); | |
let a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.style = "display: none"; | |
a.href = url; | |
a.download = `${new Date().toISOString()}.webm`; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
resetState(); | |
} | |
async function record() { | |
let stream = await navigator.mediaDevices.getUserMedia({ | |
audio: true, | |
video: true | |
}); | |
stream.getVideoTracks().forEach((x) => x.stop()); | |
stream.getAudioTracks().forEach((x) => x.stop()); | |
const devices = await navigator.mediaDevices.enumerateDevices(); | |
console.log(devices); | |
const firstAudioOutput = devices.find((device) => { | |
return device.kind === "audiooutput" | |
}); | |
stream = await navigator.mediaDevices.getUserMedia({ | |
audio: { | |
deviceId: { | |
exact: firstAudioOutput.deviceId | |
} | |
} | |
}); | |
recorder = new MediaRecorder(stream); | |
recorder.start(); | |
recorder.addEventListener("stop", download); | |
recorder.addEventListener("dataavailable", dataAvailable); | |
} | |
function stop() { | |
recorder.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment