【发布时间】:2014-02-07 21:16:16
【问题描述】:
我正在尝试使用 node.js + ffmpeg 将音频流式传输到仅使用网络音频 api 在 LAN 中连接的浏览器。
不使用元素,因为它添加了自己的 8 到 10 秒的缓冲区,我希望获得可能的最大高延迟(最大大约 1 到 2 秒)。
音频播放成功,但音频非常不连贯且嘈杂。
这是我的 node.js(服务器端)文件:
var ws = require('websocket.io'),
server = ws.listen(3000);
var child_process = require("child_process");
var i = 0;
server.on('connection', function (socket)
{
console.log('New client connected');
var ffmpeg = child_process.spawn("ffmpeg",[
"-re","-i",
"A.mp3","-f",
"f32le",
"pipe:1" // Output to STDOUT
]);
ffmpeg.stdout.on('data', function(data)
{
var buff = new Buffer(data);
socket.send(buff.toString('base64'));
});
});
这是我的 HTML:
var audioBuffer = null;
var context = null;
window.addEventListener('load', init, false);
function init() {
try {
context = new webkitAudioContext();
} catch(e) {
alert('Web Audio API is not supported in this browser');
}
}
var ws = new WebSocket("ws://localhost:3000/");
ws.onmessage = function(message)
{
var d1 = base64DecToArr(message.data).buffer;
var d2 = new DataView(d1);
var data = new Float32Array(d2.byteLength / Float32Array.BYTES_PER_ELEMENT);
for (var jj = 0; jj < data.length; ++jj)
{
data[jj] = d2.getFloat32(jj * Float32Array.BYTES_PER_ELEMENT, true);
}
var audioBuffer = context.createBuffer(2, data.length, 44100);
audioBuffer.getChannelData(0).set(data);
var source = context.createBufferSource(); // creates a sound source
source.buffer = audioBuffer;
source.connect(context.destination); // connect the source to the context's destination (the speakers)
source.start(0);
};
谁能告诉我哪里出了问题?
问候, 纳扬
【问题讨论】:
-
你好 nayan,我正在使用网络音频 api,并且想记录通过网络音频 api 播放的声音,我的问题stackoverflow.com/questions/21234902/… 在这里,你能帮帮我吗
标签: node.js ffmpeg html5-audio web-audio-api