在每隔几秒播放的动画中添加声音

Add sound to animation played every few seconds

本文关键字:播放 动画 声音 添加 几秒      更新时间:2023-09-26

我有这个铃铛每隔几秒动画一次


演示http://inspectelement.com/demos/css3/bell


的代码http://inspectelement.com/tutorials/ring-a-bell-with-css-keyframe-animations

我想添加一个铃声,我希望声音只在每次动画开始时播放

试试这个:

var soundFile = 'sound.wav';
playSound(soundFile);
setInterval(function () {
    playSound(soundFile);
}, 8000);
function playSound(audio) {
    var soundElement = '<audio style="display:none; width: 0px; height: 0px;" id="audioNotifier" src="' + audio + '" controls preload="auto" autobuffer></audio>';
    $('#audioContainer').html(soundElement);
    $('#audioNotifier')[0].play();
}

首先有一个超时。每8秒调用一次playSound函数。playSound在音频容器中设置HTML5音频播放器,播放音频。音频容器只是一个DOM元素,没有什么特别的。你的声音文件的url应该作为参数传递给playSound