Skip to content

Instantly share code, notes, and snippets.

@autione
Forked from bryanjenningz/audio-recorder-07.js
Last active July 15, 2020 00:35
Show Gist options
  • Save autione/6668616703ccf404b370f3381956a924 to your computer and use it in GitHub Desktop.
Save autione/6668616703ccf404b370f3381956a924 to your computer and use it in GitHub Desktop.
const recordAudio = () => {
return new Promise(resolve => {
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
const mediaRecorder = new MediaRecorder(stream);
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
const start = () => {
mediaRecorder.start();
};
const stop = () => {
return 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 });
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment