Created
May 3, 2018 07:36
-
-
Save rao123dk/b8da88c5742a73f5cc23de7ff411a79e to your computer and use it in GitHub Desktop.
This file contains 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
<button onClick="start()">start</button> | |
<button onClick="stop()">stop</button> | |
<button onClick="pause()">pause</button> | |
<button onClick="play()">play</button> | |
<span id="status"></span> | |
let audioChunks = []; | |
let mediaRecorder; | |
//Start the recording | |
function start(){ | |
navigator.mediaDevices.getUserMedia({ audio: true }) | |
.then(stream => { | |
mediaRecorder = new MediaRecorder(stream); | |
mediaRecorder.start(); | |
mediaRecorder.addEventListener("dataavailable", event => { | |
audioChunks.push(event.data); | |
}); | |
mediaRecorder.addEventListener("stop", () => { | |
console.log(audioChunks); | |
}); | |
}); | |
document.getElementById('status').innerHTML = "Start"; | |
} | |
//Stop the recording | |
function stop(){ | |
mediaRecorder.stop(); | |
document.getElementById('status').innerHTML = "stop" | |
} | |
//Pause the recording | |
function pause(){ | |
mediaRecorder.pause(); | |
} | |
//Play the recorded Audio | |
function play(){ | |
const audioBlob = new Blob(audioChunks); | |
const audioUrl = URL.createObjectURL(audioBlob); | |
console.log(audioUrl); | |
const audio = new Audio(audioUrl); | |
audio.play(); | |
document.getElementById('status').innerHTML = "Play" | |
} | |
// For advance you can go through | |
// https://github.com/daaain/JSSoundRecorder | |
// https://developers.google.com/web/fundamentals/media/recording-audio/ | |
// https://p5js.org/examples/sound-record-save-audio.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Soon demo will be available on https://rao123dk.github.io/speech/ .
Thanks
Dheeraj Kumar Rao (https://twitter.com/rao123dk)