爱程序网

苹果手机端触屏自动播放音乐

来源: 阅读:

//触发音乐播放和暂停,解决ios不能自动播放音乐问题
var once=1;
(function(){
    $("body").bind('touchstart',function(){
        if(once==1){
        bmusic();
        }
    })
})()
function bmusic(){

    if (window.HTMLAudioElement) {
        try {
            var oAudio = document.getElementById('myaudio');
            var btn = document.getElementById('play');
            var audioURL = document.getElementById('audiofile');

            //Skip loading if current file hasn't changed.
            if (audioURL.value !== currentFile) {
                oAudio.src = audioURL.value;
                currentFile = audioURL.value;
            }

            // Tests the paused attribute and set state.
            if (oAudio.paused) {
                oAudio.play();
                once=0;
            }

        }
        catch (e) {
            // Fail silently but show in F12 developer tools console
            if(window.console && console.error("Error:" + e));
        }
    }
}

 

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助