Skip to content

Instantly share code, notes, and snippets.

@pngan
Created February 28, 2018 18:22
Show Gist options
  • Select an option

  • Save pngan/c022c1c2152d27fd55a580bf3e4d872a to your computer and use it in GitHub Desktop.

Select an option

Save pngan/c022c1c2152d27fd55a580bf3e4d872a to your computer and use it in GitHub Desktop.
Download audio using javascript and playing in an audio instance
<html>
<head>
<script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://pn.tpagent.eilabonline.biz:8957/api/v1/recordingstream/audios/57defc71-6faf-4c98-95fb-06fe1b6acb92");
xhr.setRequestHeader("Authorization", "Bearer <token>");
xhr.responseType = 'blob';
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log("got data");
var audio = document.getElementById('target');
audio.src = URL.createObjectURL(xhr.response);
audio.play();
}
};
xhr.send(null);
</script>
</head>
<body>
<audio id="target" controls />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment