如何在带有Web音频API的浏览器上播放ArrayBuffer流?

时间:2013-11-09 19:52:15

标签: node.js audio websocket web-audio

我有一个独立的设备,它使用ffmpeg并将音频流传输到我的节点服务器。我使用ffmpeg命令对流进行编码

ffmpeg -f alsa -i hw:0 -acodec mp2 -f mp3 -r 30 http://localhost:8086/abc

输出:

ffmpeg version 0.8.8-4:0.8.8-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
built on Oct 22 2013 12:31:55 with gcc 4.6.3
[alsa @ 0x20ef9c0] Estimating duration from bitrate, this may be inaccurate
 Input #0, alsa, from 'hw:0':
  Duration: N/A, start: 10088.609013, bitrate: N/A
  Stream #0.0: Audio: pcm_s16le, 48000 Hz, 2 channels, s16, 1536 kb/s
Output #0, mp3, to 'http://localhost:8086/abc':
  Metadata:
    TSSE            : Lavf53.21.1
    Stream #0.0: Audio: mp2, 48000 Hz, 2 channels, s16, 128 kb/s
Stream mapping:
  Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
size=     462kB time=29.54 bitrate= 128.0kbits/s 

我正在使用Web套接字将流写入所有连接的客户端。流看起来像这样

request.on('data', function(data){
    console.log(data);
    audioSocket.broadcast(data, {binary:true});
});

//stream output
<Buffer 78 00 dc ff 3b 00 1e 00 27 00 0d 00 72 00 f7 ff 4f 00 0e 00 7d 00 f5 ff 43 00 e2 ff 15 00 e5 ff 3e 00 db ff e9 ff d6 ff 24 00 ad ff ad ff a3 ff a3 ff 53 ...>

我正在尝试使用网络音频API播放此流,但我无法通过decodeAudioData步骤。它总是调用错误回调,而错误总是为空。

var audio = new WebSocket('ws://localhost:8088/');
audio.binaryType = "arraybuffer";
var context = new webkitAudioContext();

audio.onmessage = function(data){
    var buf  = data.data;
    d = data;
    context.decodeAudioData(
        buf,
        function(buffer){    console.log(buffer);    },     //success handler
        function(err){   console.log('error: ', err)    }   //error handler
    );
}

如何播放此流?

1 个答案:

答案 0 :(得分:0)

听起来您正在尝试创建媒体流,而不是解码实际文件。我可能是错的,但我可以想象您的套接字正在流式传输数据,而不是纯粹的“加载”,在这种情况下,decodeAudioData不能满足您的需求。查看AudiContext.createMediaStreamSource API。