Skip to content

Instantly share code, notes, and snippets.

@hadpro24
Last active February 21, 2021 01:51
Show Gist options
  • Save hadpro24/5392867fee1b3bc1746920b4fc3a59a1 to your computer and use it in GitHub Desktop.
Save hadpro24/5392867fee1b3bc1746920b4fc3a59a1 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
var recorder;
const record = async() =>{
let localStream
try {
localStream = await navigator.mediaDevices.getUserMedia({
audio: true
})
} catch (error) {
console.log(error)
}
recorder = new MediaRecorder(localStream);
recorder.start();
}
const stopRecord = () => {
//
recorder.stop();
recorder.ondataavailable = function(e) {
var url = URL.createObjectURL(e.data);
var preview = document.querySelector('audio');
preview.autoplay = true;
preview.src = url;
// http call
console.log(e.data, url);
};
}
document.querySelector('button').addEventListener('mouseup', stopRecord);
document.querySelector('button').addEventListener('mousedown', record);
/// stop record
stream.getTracks().forEach(function(track) {
track.stop();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment