I am using the Modernizr code as displayed here http://codepen.io/davidgenetic/pen/FmHaD to check if autoplay is supported. This works when the site is viewed the first time on Safari Desktop (v 9.0.1), but fails after that (incorrectly concludes that autoplay is not supported when it is). Deleting the cache, and reloading page makes it work again. On other browsers (Chrome, IE, Firefox, Opera) this works fine. Does anyone know how to solve this problem?
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;});