Last active
November 12, 2024 17:34
-
-
Save praveenpuglia/61a296064815f19f391f2c84f12f134b to your computer and use it in GitHub Desktop.
Download Audio from AJAX and Play as Blob
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
// Updated on 16th May, 2024 based on @chamie's suggestion. | |
const response = await fetch("http://path/to/audio.wav"); | |
const data = await response.arrayBuffer(); | |
const blob = new Blob([data], { type: "audio/wav" }); | |
const blobUrl = URL.createObjectURL(blob); | |
const audio = new Audio(); | |
audio.src = blobUrl; | |
audio.controls = true; | |
document.body.appendChild(audio); | |
// Optionally, if you want to start playing right away: | |
audio.play(); |
@yairMoshkovitz that's because one .read()
call only gives you a ≈4k bytes chunk.
Thankfully, you don't need it at all nowadays, here's how you could achieve this in 2024:
const response = await fetch("http://path/to/audio.wav");
const data = await response.arrayBuffer();
const blob = new Blob([data], { type: "audio/wav" });
const blobUrl = URL.createObjectURL(blob);
const audio = new Audio();
audio.src = blobUrl;
audio.controls = true;
document.body.appendChild(audio);
//Optionally, if you want to start playing right away:
audio.play();
Thanks @chamie. Updating the code with your suggestion.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks!
i have a problom, this function load only few secondes.
can you halp my?