我有一个经过修改的 jPlayer,它连接到shoutcast/icecast 源。
我想让 jPlayer 在打开页面时随机连接 2 个或多个 url。
例如,如果我有 2 个链接:
- www.example.com:8000/live
- www.example.com:8000/live2
然后我需要jPlayer在打开页面时随机选择其中一个。 目的是防止一台服务器过载。
我应该如何在 jQuery 代码中处理它?</p>
jQuery:
$("#jquery_jplayer_1").jPlayer({
ready: function(event) {
$(this).jPlayer("setMedia", {
mp3: "http://www.example.com:8000/live"
}).jPlayer("play");
},
swfPath: "js/",
wmode: "window",
solution: "flash,html",
supplied: "mp3",
preload: "none",
volume:0.75,
cssSelectorAncestor: "",
cssSelector: {
play: "#play",
pause: "#pause"
}
});
$("#jquery_jplayer_1").bind($.jPlayer.event.pause, function(event) {
$(this).jPlayer("clearMedia");
$(this).jPlayer("setMedia", {
mp3: "http://www.example.com:8000/live"
});
});
最佳答案
回答您的问题时,您可以执行以下操作。
var servers = ["www.example.com:8000/live", "www.example.com:8000/live2"];
var server = servers[Math.floor(Math.random() * servers.length)];
$(this).jPlayer("setMedia", {
mp3: server
});
<小时/>
尽管这不是解决您实际问题的最佳方法:
The purpose is to prevent over-load on one server.
您应该考虑使用load balancing .
关于javascript - jPlayer 中的随机流链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13980060/