且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

的JavaScript + HTML5:音频只在特定情况下打一次

更新时间:2022-11-03 10:34:50

根据我自己的一些试验和错误,我建议想一旦它发挥到currentTime属性重置为0,并再次尝试。

Based on some of my own trial-and-error, I'd suggest trying to reset the currentTime property to 0 once it has played and try it again.

我发现某些情况下(和你的可能是其中之一),其中音频卡和结束,本身不复位。

I've found some cases (and yours might be one of them) where the audio gets stuck and the end and doesn't reset by itself.

这是实际的解释我不能提供...但如果​​它像我见过的 - 这可能是一个可行的解决方案。

An actual "explanation" I can't offer...but if it's like the ones I've seen - this might be a viable solution.

<audio id="foo"><source url="myaudio.mp3"></audio>
...
var soundFoo = document.getElementById('foo');
soundFoo.play();
soundFoo.onended = function() {
  soundFoo.currentTime = 0;
  soundFoo.play(); // assuming you want it to repeat right away.
}