我正在使用此处显示的Modernizr代码http://codepen.io/davidgenetic/pen/FmHaD来检查是否支持自动播放。这在第一次在Safari桌面(v 9.0.1)上查看站点时有效,但在此之后失败(错误地断定当它不支持自动播放时)。删除缓存并重新加载页面使其再次起作用。在其他浏览器(Chrome,IE,Firefox,Opera)上,这很好用。有谁知道如何解决这个问题?
Modernizr.addTest('autoplay', function(){
// Audio file data URIs from comments in
// [this gist](https://gist.github.com/westonruter/253174)
// via [mudcube](https://github.com/mudcube)
var mp3 = 'somesong.mp3';
try {
var audio = new Audio();
var src = audio.canPlayType('audio/ogg') ? ogg : mp3;
audio.autoplay = true;
audio.volume = 0;
// this will only be triggered if autoplay works
audio.addEventListener('play', function() {
Modernizr.autoplay = true;
// is there a better way to re-evaluate the html classes added by Modernizr?
var root = document.getElementsByTagName('html')[0];
root.classList.remove('no-autoplay');
root.classList.add('autoplay');
// or if you're using jQuery:
// $('html').toggleClass('no-autoplay autoplay');
}, false);
audio.src = src;
} catch(e) {
console.log('[AUTOPLAY-ERROR]', e);
}
return false;});