Created
October 7, 2017 20:25
-
-
Save bryanjenningz/a2d67a386e0477d24ff394087527c5fa to your computer and use it in GitHub Desktop.
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
const recordAudio = () => | |
new Promise(async resolve => { | |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); | |
const mediaRecorder = new MediaRecorder(stream); | |
const audioChunks = []; | |
mediaRecorder.addEventListener("dataavailable", event => { | |
audioChunks.push(event.data); | |
}); | |
const start = () => mediaRecorder.start(); | |
const stop = () => | |
new Promise(resolve => { | |
mediaRecorder.addEventListener("stop", () => { | |
const audioBlob = new Blob(audioChunks); | |
const audioUrl = URL.createObjectURL(audioBlob); | |
const audio = new Audio(audioUrl); | |
const play = () => audio.play(); | |
resolve({ audioBlob, audioUrl, play }); | |
}); | |
mediaRecorder.stop(); | |
}); | |
resolve({ start, stop }); | |
}); | |
const sleep = time => new Promise(resolve => setTimeout(resolve, time)); | |
(async () => { | |
const recorder = await recordAudio(); | |
recorder.start(); | |
await sleep(3000); | |
const audio = await recorder.stop(); | |
audio.play(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment