Created
October 11, 2017 18:43
-
-
Save jchang419/ab06af61cebbd51eff2da91b3f93c2c5 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<script src="https://static.opentok.com/v2/js/opentok.js"></script> | |
</head> | |
<body> | |
<button>Record audio</button> | |
<audio controls></audio> | |
<script> | |
// replace these values with those generated in your TokBox Account | |
var apiKey = ""; | |
var sessionId = ""; | |
var token = ""; | |
var clicked = false; | |
var b = document.querySelector("button"); | |
var chunks = []; | |
var ac = new AudioContext(); | |
var dest = ac.createMediaStreamDestination(); | |
var mediaRecorder = new MediaRecorder(dest.stream); | |
var session = OT.initSession(apiKey, sessionId); | |
// Subscribe to a newly created stream | |
session.on('streamCreated', function(event) { | |
session.subscribe(event.stream, 'subscriber', { | |
insertMode: 'append', | |
width: '100px', | |
height: '100px' | |
}, function(error) { | |
if (error) { | |
alert(error.message); | |
} | |
var vid = document.getElementsByTagName("video"); | |
vid = vid[0]; | |
audioSrc = ac.createMediaStreamSource(vid.srcObject); | |
audioSrc.connect(dest); | |
}); | |
}); | |
// Connect to the session | |
session.connect(token, function(error) { | |
// If the connection is successful, initialize a publisher and publish to the session | |
if (error) { | |
handleError(error); | |
} else { | |
//session.publish(publisher, handleError); | |
console.log("connected"); | |
} | |
}) | |
.on('streamDestroyed', event => { | |
console.log(event); | |
}); | |
// Handling all of our errors here by alerting them | |
function handleError(error) { | |
if (error) { | |
alert(error.message); | |
} | |
} | |
b.addEventListener("click", function(e) { | |
if (!clicked) { | |
mediaRecorder.start(); | |
e.target.innerHTML = "Stop recording"; | |
clicked = true; | |
} else { | |
mediaRecorder.stop(); | |
e.target.disabled = true; | |
} | |
}); | |
mediaRecorder.ondataavailable = function(evt) { | |
// push each chunk (blobs) in an array | |
chunks.push(evt.data); | |
}; | |
mediaRecorder.onstop = function(evt) { | |
// Make blob out of our blobs, and open it. | |
var blob = new Blob(chunks, { 'type': 'audio/ogg; codecs=opus' }); | |
document.querySelector("audio").src = URL.createObjectURL(blob); | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment