看着previous answer我想到了重试播放声音的想法:
soundBG.pause();
soundBG.currentTime = 0;
var soundPromise = soundBG.play();
if (soundPromise !== undefined) {
soundPromise.then(function() {
console.log('Sound!');
}).catch(function(error) {
console.error('Failed to start your sound, retrying.');
setTimeout(function(){
soundEffects();
}, 2000);
});
}
但它仍无法正常工作,当声音不起作用时,我不断收到错误:
DOMException:无法加载,因为找不到支持的源
无论如何要解决这个问题?
答案 0 :(得分:0)
我对音频类型文件有同样的错误并使用它
var url = document.querySelector('audio').src;
var audio = new Audio();
audio.src = url;
var playPromise = audio.play();
if (playPromise !== undefined) {
playPromise.then(function() {
audio.addEventListener('timeupdate',function() {
console.log(audio.currentTime, audio.duration);
}, true);
}).catch(function(error) {
console.error('Failed to start your sound, retrying.');
});
}