Skip to content

Instantly share code, notes, and snippets.

@saiprasad1996
Created June 19, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saiprasad1996/be0e62cb0d13691668f47fa7e398d6f8 to your computer and use it in GitHub Desktop.
Save saiprasad1996/be0e62cb0d13691668f47fa7e398d6f8 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=720, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Audio recording test</title>
</head>
<body>
<video id="src" src="chrome.webm"></video>
<video id="dest"></video>
<audio controls id="audio"></audio>
<button id="start" onclick="start()">start</button>
<button id="stop" onclick="stop()">stop</button>
<button id="play" onclick="play()">play</button>
<script type="text/javascript">
let stream = null;
let chunks = [];
let recorder = null;
let srcvideo = document.getElementById('src')
let destvideo = document.getElementById('dest')
let audiotag = document.getElementById('audio')
async function start(){
let datastream = srcvideo.captureStream()
stream = new MediaStream([...datastream.getAudioTracks()])
recorder = new MediaRecorder(stream,{mimeType:'audio/webm'})
srcvideo.play()
recorder.start()
recorder.addEventListener('dataavailable',(event)=>{
chunks.push(event.data)
})
recorder.addEventListener('stop',()=>{
srcvideo.stop()
const blob = new Blob(chunks)
let url = URL.createObjectURL(blob,{type:'audio/webm'})
audiotag.src = url
})
}
function stop(){
recorder.stop()
}
function play(){
audiotag.play()
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment