<html>
<head>
<meta charset="utf-8">
<title>跨浏览器实现音频</title>
</head>
<body>
<audio id="main_audio" preload="auto" loop="loop" volume="1.0" style="position: absolute; z-index: 20000" autoplay="true">
<source src="http://tyl.yy.com/s/qxz/img/bgm.mp3" type="audio/mpeg">
<object id="main_audio_ie8" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" style="display:none">
<param name="URL" value="http://tyl.yy.com/s/xcyz/img/bgm.mp3">
<param name="uiMode" value="invisible">
<param name="autoStart" value="true">
<param name="volume" value="100">
<param name="playCount" value="2147483647">
</object>
</audio>
<div id="controls" class="controls music on-off-music">
<span></span>关闭音乐
</div>
<script>
var isPlaying, audio = document.getElementById('main_audio');
if (audio.play instanceof Function) isPlaying = function(audio) {
return !audio.paused;
};
else {
audio = document.getElementById('main_audio_ie8');
isPlaying = function(audio) {
return audio.playState == 3;
};
audio.play = function() {
this.controls.play();
}
audio.pause = function() {
this.controls.pause();
}
}
document.querySelector('.on-off-music').onclick = function() {
if (isPlaying(audio)) {
audio.pause();
this.innerHTML = "<span style='background-image:url(http://tyl.yy.com/s/qxz/img/music-on.png)'></span>开启音乐";
} else {
audio.play();
this.innerHTML = "<span style='background-image:url(http://tyl.yy.com/s/qxz/img/music-off.png)'></span>关闭音乐";
}
};
</script>
</body>
</html>
跨浏览器实现音频
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 这篇文章中总结了SVG、Canvas、Local Storage、Web Sockets、File Api等各个方...