我正在尝试在HTML5中创建一个简单的循环记录程序。我已经完成了一些基础操作,但是现在遇到的问题是,当我尝试递归播放音频时,它会在音频完成之前或之后一直触发该方法。
我试图设置一个setTimeout,但是那只会让我感到困惑,因为我认为正确的间隔只会让我更加困惑。有人可以看看这个吗?
您还可以找到一个活动站点http://rossb.byethost11.com/
/*
* finds duration of sound file in milliseconds
*/
function getMasterDuration() {
var sound = document.getElementById("session-1");
var returnValue = sound.duration * 1000;
return returnValue;
}
/*
* called from the button
*/
function playAllService() {
//set up recursive loop to play sounds
window.setInterval(recursivePlay, getMasterDuration);
}
function recursivePlay() {
//play all sounds
playAll();
//set timeout until sound is done (?)
setTimeout(recursivePlay, getMasterDuration);
}
/*
* Finds all the recording sessions and plays them.
*/
function playAll() {
for (var i = 1; i <= numberOfSessions; i++) {
play(i);
}
}
是的,我知道它需要清理。
谢谢您的帮助!
编辑说明:我希望它循环播放而不会中断。像是环形花瓣或鼓机。
当您访问不带括号的函数时,就是在指该函数本身。因此,调用将recursivePlay
返回以下内容:
function recursivePlay() {
//play all sounds
playAll();
//set timeout until sound is done (?)
setTimeout(recursivePlay, getMasterDuration);
}
这对于的第一个参数是正确的setTimeout
,因为您希望函数在一定间隔后运行。
的第二个参数setTimeout
应为数字(以毫秒为单位)。但是,您返回的是函数 getMasterDuration
而不是函数的结果。
要解决此问题,只需在括号中添加括号getMasterDuration
:
setTimeout(recursivePlay, getMasterDuration());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句