Skip to content

Instantly share code, notes, and snippets.

@rao123dk
Created May 3, 2018 07:36
Show Gist options
  • Save rao123dk/b8da88c5742a73f5cc23de7ff411a79e to your computer and use it in GitHub Desktop.
Save rao123dk/b8da88c5742a73f5cc23de7ff411a79e to your computer and use it in GitHub Desktop.
<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
@rao123dk
Copy link
Author

rao123dk commented May 9, 2018

Soon demo will be available on https://rao123dk.github.io/speech/ .
Thanks
Dheeraj Kumar Rao (https://twitter.com/rao123dk)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment