Some time ago, I've posted a snippet for making client-side-only beeps from JavaScript. I have an update to that.
Back at the time, I've used a data: URL with a Base64-encoded buffer containing a WAV. Since then, I've learned about an even better URL schema - blob:. One of those can be constructed on top of a buffer-like object such as an ArrayBuffer. Endow it with a MIME type, and play away. You no longer need a Base64 encoder. I've updated the gist accordingly. The only difference it made was the final line, where the URL is generated; instead of"data:audio/wav;base64," + ToBase64(wav)
it became
URL.createObjectURL(new Blob([wav], {type: "audio/wav"}));
and the Base64 encoder is no longer needed, and extra couple of rounds of data copying is avoided.
If the page in question is being served from a server which sets the Content-Security-Policy HTTP header, you'll need a media-src blob: clause in it.
No comments:
Post a Comment