Skip to content

Instantly share code, notes, and snippets.

@goofmint
Created Feb 20, 2018
Embed
What would you like to do?
function startRecording(stream, lengthInMS) {
let recorder = new MediaRecorder(stream);
let data = [];
recorder.ondataavailable = event => data.push(event.data);
recorder.start();
let stopped = new Promise((resolve, reject) => {
recorder.onstop = resolve;
recorder.onerror = event => reject(event.name);
});
let recorded = new Promise((resolve, reject) => {
setTimeout(() => {
recorder.state == "recording" && recorder.stop()
resolve();
}, lengthInMS);
});
return Promise.all([
stopped,
recorded
])
.then(() => data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment