如何使用节点服务器制作实时音频流?
How can I make a living audio streaming with node server?
我想以音频流媒体为生。我从节点服务器通过管道传输我的 mp3 文件,没有问题。我在客户端播放 mp3,但它只在流结束时播放。我想在流继续时播放它。我该怎么做?
服务器端
var stream = ss.createStream();
ss(socket).emit('H_to_S_shareVoice', stream);
var filename = 'musix' + '.mp3';
console.log(filename);
fs.createReadStream(filename).pipe(stream);
客户
<br>
<br>
<h1 id='socket'>not connected</h1>
<br>
<audio autoplay volume="1" controls id="audio" />
$(document).ready(function () {
$(function () {
var socket = io.connect('http://localhost:4000');
socket.on("connect", function () {
console.log("on connect");
document.getElementById('socket').innerHTML = 'connected to the server';
ss(socket).on('H_to_S_shareVoice', function (stream, data) {
console.log('received', data);
var binaryString = "";
stream.on('data', function (data) {
console.log('data')
console.log(data.length)
for (var i = 0; i < data.length; i++) {
binaryString += String.fromCharCode(data[i]);
}
$("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
document.getElementById('audio').load();
document.getElementById('audio').play();
//document.getElementById('test').innerHTML = window.btoa(binaryString);
/*if (dat.length > 0 && oneTime) {
oneTime = false;
}*/
// $("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
binaryString = ""
});
/*stream.on('end', function (data) {
console.log('end')
document.write(binaryString);
$("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
binaryString = "";
});*/
});
});
});
});
</script>
删除所有 Socket.IO 代码,整个 base64 编码一团糟...
您所要做的就是在收到请求时通过 HTTP 输出媒体数据。您的客户端代码就像 <audio src="path/on/the/server"></audio>
.
一样简单
我用 PCMPlayer 修复了。
var player = new PCMPlayer({
encoding: '16bitInt',
channels: 2,
sampleRate: 16000,
flushingTime: 1000
});
stream.on('data', function (data) {
console.log(data)
player.feed(data);
});
我想以音频流媒体为生。我从节点服务器通过管道传输我的 mp3 文件,没有问题。我在客户端播放 mp3,但它只在流结束时播放。我想在流继续时播放它。我该怎么做?
服务器端
var stream = ss.createStream();
ss(socket).emit('H_to_S_shareVoice', stream);
var filename = 'musix' + '.mp3';
console.log(filename);
fs.createReadStream(filename).pipe(stream);
客户
<br>
<br>
<h1 id='socket'>not connected</h1>
<br>
<audio autoplay volume="1" controls id="audio" />
$(document).ready(function () {
$(function () {
var socket = io.connect('http://localhost:4000');
socket.on("connect", function () {
console.log("on connect");
document.getElementById('socket').innerHTML = 'connected to the server';
ss(socket).on('H_to_S_shareVoice', function (stream, data) {
console.log('received', data);
var binaryString = "";
stream.on('data', function (data) {
console.log('data')
console.log(data.length)
for (var i = 0; i < data.length; i++) {
binaryString += String.fromCharCode(data[i]);
}
$("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
document.getElementById('audio').load();
document.getElementById('audio').play();
//document.getElementById('test').innerHTML = window.btoa(binaryString);
/*if (dat.length > 0 && oneTime) {
oneTime = false;
}*/
// $("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
binaryString = ""
});
/*stream.on('end', function (data) {
console.log('end')
document.write(binaryString);
$("#audio").attr("src", "data:audio/wav;base64," + window.btoa(binaryString));
binaryString = "";
});*/
});
});
});
});
</script>
删除所有 Socket.IO 代码,整个 base64 编码一团糟...
您所要做的就是在收到请求时通过 HTTP 输出媒体数据。您的客户端代码就像 <audio src="path/on/the/server"></audio>
.
我用 PCMPlayer 修复了。
var player = new PCMPlayer({
encoding: '16bitInt',
channels: 2,
sampleRate: 16000,
flushingTime: 1000
});
stream.on('data', function (data) {
console.log(data)
player.feed(data);
});