html:
<audio src='xxx.mp3' id='audio'></audio>
状态切换:
//控制音频
function playOrPause(elemId,type) {
var elem = document.getElementById(elemId);
//停止
if (type==0){
elem.pause();
elem.currentTime = 0; //如只暂停则不需要此行
}
//播放
else if (type==1){
elem.play();
}
//切换
else {
if (elem.paused) {
elem.play();
}
else {
elem.pause();
elem.currentTime = 0;
}
}
}
播放次数:
var elem,start,times;
//播放次数
function playTimes(elemId, count) {
elem = document.getElementById(elemId);
start = 0;
times = count;
elem.addEventListener("ended",playFunction);
elem.play();
}
function playFunction() {
start++;
// alert(start);
if(start<times){
elem.play();
}
}