在网上许多类似的脚本关于音乐播放;
但是这些脚本真的太老了,真想骂一句,你这些脚本兼容吗?!现在IE6,7,8,9,Firefox,Chrome,这些目前是非常常用的;
总不能局限于国内的IE6,7吧;
好了不废话了,
推荐我的做法:
<script type="text/JavaScript" src="js/swfobject.js"></script>
<script type="text/javascript">
var arr = [
"http://ting.9ku.com//file2/2011/2/28/2.mp3",
"http://ting.9ku.com//file2/2011/5/31/99.mp3"
];
window.onload = function(){
var img = [];
for(var i = 0, len = arr.length; i < len; i++ ){
img[i] = new Image();
img[i].src = arr[i];
}
createPlayer();
}
</script>
把这一段代码放在head里,预先加载代码及mp3音乐;
下一步:
<script type="text/javascript">
/* <![CDATA[ */
var player = null;
var currentPosition = 0;
var currentVolume = 80;
function playerReady(thePlayer) {
player = window.document[thePlayer.id];
}
function createPlayer() {
var flashvars = {
file:arr[1],
autostart:"false",
start:"5",
//controlbar:"none",
playlist:"none" ,
bufferlength:"1",
volume:"100"
}
var params = {
allowfullscreen:"true",
allowscriptaccess:"always"
}
var attributes = {
id:"player1",
name:"player1"
}
swfobject.embedSWF("js/player.swf", "placeholder1", "350", "100", "9.0.115", false, flashvars, params, attributes);
}
/* ]]> */
</script>
以上代码是播放控件的脚本;相信这些简单的英文单词都看的懂的,如果真不懂就那copy吧,无版本,呵呵。
以下是body 一块:
<body >
<h2>播放第一首歌 伤不起 (王麟)</h2>
<div class="playBtn">
<a href="#" οnclick="player.sendEvent('LOAD',
arr[1]);
player.sendEvent('PLAY');
">load video 1</a>
<h2>播放第二首歌 红尘情歌 (高安)</h2>
<a href="#" οnclick="player.sendEvent('LOAD',
arr[0]);player.sendEvent('PLAY');
player.sendEvent('START',40);">load video 2</a><br/><br/>
<a href="#" οnclick="player.sendEvent('PAUSED');player.sendEvent('LOAD',
arr[0]);
">Stop</a>
</div>
<br/>
<div id="wrapper">
<div id="placeholder1"></div>
</div>
</body>
好了,保存页面即可:
这里要说明一点,此jwplayer需要在服务器上测试,不可直接打开。否则不关偶的事了;