如何通过websocket直播mp3
var AudioType = "audio/mp3";
var protocol = "ws://";
var host = "127.0.0.1";
var port = 4446;
var uri = protocol + host + ":" + port + "/"
var ws = new WebSocket(uri);
ws.onopen = function() {
console.log("Connected");
ws.send(JSON.stringify({"reqID3":1}));
ws.send(JSON.stringify({"init":1}))
}
ws.onmessage = function(e){
var js = JSON.parse(e.data);
if (js.id3) {
document.getElementById("bitrate").innerHTML = js.id3.bitrate;
document.getElementById("length").innerHTML = js.id3.length;
document.getElementById("title").innerHTML = js.id3.title;
}
if (js.buffer) {
//console.log(js.buffer)
var b = js.buffer
var audio = document.getElementById("audio");
audio.type = AudioType;
audio.src = "data:audio/mp3;base64,"+b
}
}
ws.onclose = function() {
ws.close()
}
请注意audio.src
会加载长度为4096的base64数据的问题是流无法继续播放,每次加载新的base64字符串时都会重新启动,是否有解决方法,甚至可以通过websockets进行操作< / p>