我有一个使用 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
我正在使用网络套接字然后将流写入所有连接的客户端。流看起来像这样
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 步骤。它总是调用错误回调并且 err 总是为空。
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
);
}
我怎样才能播放这个流?