Javascript 流式播放Icecast电台时检测歌曲变化
我正在使用JPlayer播放一个Icecast电台,并希望在曲目更改时显示当前歌曲名称。我有一个PHP,它使用一个函数来检测当前播放的曲目 我想在每次曲目更改时更新曲目标题和乐队图像。我无法找到一种方法在每次歌曲更改时触发事件,但我知道歌曲元数据在流中每16000字节发送一次 有没有办法使用Javascript检查包含歌曲信息的元数据块,从而允许我在播放新曲目时更改歌曲标题和相应的google图像 我当前的代码如下,我正在ping脚本,以便每分钟获取当前曲目:Javascript 流式播放Icecast电台时检测歌曲变化,javascript,html,audio-streaming,jplayer,icecast,Javascript,Html,Audio Streaming,Jplayer,Icecast,我正在使用JPlayer播放一个Icecast电台,并希望在曲目更改时显示当前歌曲名称。我有一个PHP,它使用一个函数来检测当前播放的曲目 我想在每次曲目更改时更新曲目标题和乐队图像。我无法找到一种方法在每次歌曲更改时触发事件,但我知道歌曲元数据在流中每16000字节发送一次 有没有办法使用Javascript检查包含歌曲信息的元数据块,从而允许我在播放新曲目时更改歌曲标题和相应的google图像 我当前的代码如下,我正在ping脚本,以便每分钟获取当前曲目: $(document).ready
$(document).ready(function(){
var stream = {
title: "My Stream",
mp3: "http://mystream.com:8000/myStream"
},
ready = false;
$("#jquery_jplayer_1").jPlayer({
ready: function (event) {
ready = true;
$(this).jPlayer("setMedia", stream).jPlayer("play");
},
pause: function() {
$(this).jPlayer("clearMedia");
},
error: function(event) {
if(ready && event.jPlayer.error.type === $.jPlayer.error.URL_NOT_SET) {
$(this).jPlayer("setMedia", stream).jPlayer("play");
}
},
stop: function(event) {
console.log("stop");
$(this).jPlayer("setMedia", stream).jPlayer("play");
},
swfPath: "js",
supplied: "mp3",
preload: "none",
autoPlay: true,
wmode: "window",
keyEnabled: true
});
requestTrack();
setInterval(requestTrack, 60000);
// get the current track info
function requestTrack() {
$.get( "tools/getTrack.php", function( data ) {
console.log(data);
$( "#current-track" ).html( data );
// search for an image with the band/song name
$.get( "https://www.googleapis.com/customsearch/v1?key=MY_KEY&cx=MY_CX&q="+encodeURIComponent(data)+"&searchType=image", function( data ) {
console.log(data.items[0].link);
$("#album").attr("src",data.items[0].link);
});
});
}
});
在JavaScript中无法检测到元数据块。事实上,浏览器根本不需要它。您必须在服务器端执行此操作。您可以实现轮询,或者在服务器上修改脚本,以便在元数据更改时将数据推送到客户端。这需要从HTTP服务器持续连接到Icecast/SHOUTcast服务器