首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webkitAudioContext回放base64 WAV

webkitAudioContext回放base64 WAV
EN

Stack Overflow用户
提问于 2012-10-25 21:36:17
回答 1查看 1.2K关注 0票数 0

我有一个wav文件,它与:ffmpeg -i my.wav

给出输出:

代码语言:javascript
复制
output:   Duration: 00:00:12.63, bitrate: 352 kb/s
    Stream #0.0: Audio: pcm_s16le, 22050 Hz, 1 channels, s16, 352 kb/s

它作为base64字符串存储在内存中的JavaScript中,如下面列出的base64变量所示。

以下是代码:

代码语言:javascript
复制
byte_str = Base64.decode(base64)
buffer = new ArrayBuffer(byte_str.length*2)
buffer_view = new Uint16Array(buffer)
/* this is coffeescript */
for i in [0..byte_str.length]
   buffer_view[i] = byte_str.charCodeAt(i)
console.log(byte_str.length)
console.log(buffer_view)
console.log(buffer_view.buffer)
context = new webkitAudioContext()
context.decodeAudioData(buffer,function (buffer) {
    /* never gets hit */
    source = @context.createBufferSource()
    source.buffer = buffer
    source.connect(@context.destination)
    source.noteOn(0)
});

输出:

代码语言:javascript
复制
108185
[82, 73, 70, 70, 36, 128, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 34, 86, 0, 0, 68, 49152, 2, 0, 16, 0, 100, 97, 116, 97, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,....]
ArrayBuffer {byteLength: 216370} 

如果您查看这个演示文稿,它有一个WAV头:http://html5-demos.appspot.com/static/html5-whats-new/template/index.html#29

代码语言:javascript
复制
var header = new Uint8Array([
    0x52,0x49,0x46,0x46, // "RIFF"
    0, 0, 0, 0,          // put total size here
    0x57,0x41,0x56,0x45,
    ...
]);

如果将第一个值转换为十六进制,则与我的相同,但在我的回调中,decodeAudioData不会触发回调。有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-18 00:38:28

事实证明,这是一个使用这个库的编码问题:http://www.webtoolkit.info/javascript-base64.html,我删除了_utf8_decode步骤,它运行得很好!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13077682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档