Feature Detect Autoplay HTML5 Audio - Audio on Mobile Browsers

后端 未结 4 1646
滥情空心
滥情空心 2021-02-07 11:50

So I have a site where users would want to demo audio output produced by a server side script. They select some options and hit a create button. I then AJAX in a HTML5 audio ele

4条回答
  •  清酒与你
    2021-02-07 12:26

    Help for future answer seekers, an ideal way to check if auto-play supported or not by the browsers/devices:

    var promise = document.getElementById('video').play();
    
    if( typeof promise !== 'undefined' ) {
        promise.then(function() {
            // auto-play started!
        }).catch(function(error) {
            // auto-play failed
        });
    }
    

    Please note, media play promise is not supported by all browsers yet. In case if browser doesn't support it, it will return undefined.

提交回复
热议问题