Javascript 制作音频 blob [英] Javascript make audio blob
问题描述
我正在测试 html 音频标签,我想制作音频 blob 网址,就像 youtube 或 vimeo 一样,并将其添加到 "src" 以开始播放音频.
I am testing the html audio tag and I would like to make audio blob url's, like youtube or vimeo does, and add it to the "src" to start playing the audio.
我一直在使用 new Blob()
和 URL.createObjectURL()
进行测试,但我不知道如何使用它.
I've been testing with new Blob()
and URL.createObjectURL()
but I don't know how to use it.
我想做这样的事情:<音频控件src"blob:null/8142a612-29ad-4220-af08-9a31c55ed078"></audio>
非常感谢您的帮助.
推荐答案
假设你有这样的 base64 编码版本的音频
Suppose you have base64 encoded version of audio like this
data="data:audio/ogg;base64,T2dnUwACAAAAAAAAAADSeWyXAU9nZ1MAAAAAAAAAAAAA0nl";
1.首先删除 base64 标头(前导码)并将其解码为纯二进制形式,即它在 iPad 中的形式.您可以使用 convertDataURIToBinary 由 borismus 在 github 上
1.First remove the base64 headers (preamble) and decode it to pure binary form, the form it lies in as in your iPad. You can use convertDataURIToBinary an excellent snippet by borismus on github for that
var binary= convertDataURIToBinary(data);
2.现在从二进制创建一个 Blob;指定音频的类型
2.Now create a Blob from the binary; specifying the type of audio it is
var blob=new Blob([binary], {type : 'audio/ogg'});
3.现在从这个 Blob 中创建 Blob url
3.Now create blob url out of this Blob
var blobUrl = URL.createObjectURL(blob);
现在只需将 <source>
的 src
属性替换为这个 blob url.如果您已经拥有纯解码的二进制文件,那么您只需执行第 3 步
That's all now simply replace src
attribute of <source>
to this blob url.In case you already have the pure decoded binary then you just do step 3
https://jsfiddle.net/sanddune/uubnnr0w/
这篇关于Javascript 制作音频 blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!