我有一个让声音通过计时器反复播放的js程序,并且在我的电脑上工作得非常好,但是,当我尝试在我的手机(IPhone 13)或平板电脑(IPad air 4)上使用它时,它不起作用。声音不是晚了就是不放了。如果它提供了任何线索,我还注意到,如果我在程序运行时改变了手机的系统音量(手机一侧有物理按钮),音量条就会滞后和跳跃,而不是平稳地增加或减少。
安装以允许声音在ios上播放(因为它们需要用户交互)
var click1 = new Audio('../audio/click1.mp3');
click1.preload = "auto";
enterBtn.addEventListener('click', () => {
//...code
click1.volume = 0;
click1.play();
click1.pause();
click1.currentTime = 0;
click1.volume = 1;
});
当定时器调用它时运行的代码。
function playClick() {
click1.load();
click1.play();
click1.currentTime = 0;
//...code
}
从堆栈溢出中获取的计时器代码
function Timer(callback, timeInterval, options) {
this.timeInterval = timeInterval;
this.start = () => {
this.expected = Date.now() + this.timeInterval;
this.theTimeout = null;
if (options.immediate) {
callback();
}
this.timeout = setTimeout(this.round, this.timeInterval);
console.log('Timer Started');
}
this.stop = () => {
clearTimeout(this.timeout);
}
this.round = () => {
var drift = Date.now() - this.expected;
if (drift > this.timeInterval) {
if (options.errorCallback) {
options.errorCallback();
}
}
callback();
this.expected += this.timeInterval;
this.timeout = setTimeout(this.round, Math.max(0,this.timeInterval - drift));
}
}
export default Timer;
发布于 2022-05-07 14:40:20
用setInterval
代替计时器怎么样?https://developer.mozilla.org/en-US/docs/Web/API/setInterval
https://stackoverflow.com/questions/72156793
复制相似问题