在做一个播放按钮时突然用到了这个技术,具体还是蛮简单的,一看就会,就是那个音频文件的src属性动态赋值的时候花了点时间。看代码吧!
function playOrPaused(obj) {
var url = "f:/lrh.mp3";
//alert(url);
var audio = document.getElementById("audio");
if (audio) {
}
else {
var board = document.getElementById("_button");
var a2 = document.createElement("audio");
a2.setAttribute("src", url);
a2.setAttribute("controls", "controls");
a2.setAttribute("id", "audio");
audio = board.appendChild(a2);
}
if (audio.paused) {
audio.play();
document.getElementById("btn").value = "暂停";
return;
} else {
audio.pause();
document.getElementById("btn").value = "播放";
}
}
<body>
<form id="form1" runat="server">
<div>
<div id="_button" runat="server" style="width: 100%; height: 40px;">
<input id="btn" type="button" value="播放" style="height: 30px; width: 80px; margin-top: 2px" onclick="playOrPaused(this);" />
</div>
</div>
</form>
</body>