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

//触发音乐播放和暂停,解决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));

        }

    }

}

 

你可能感兴趣的:(音乐)