Original link:
http://www.cnblogs.com/qdlhj/p/10749991.html
In the micro letter, ios phone is automatically cut out the following music can not be played automatically, we can execute the elements on the trigger body, automatically playing.
//音乐 var x = document.getElementById("myAudio"); var at=$("#myAudio") var audobtn=true; function playAudio(at){ var faf=at[0]; faf.play();//播放 $(".audio-btn").addClass("move1"); } var autopa=true; $("body").on("click",function(){ if(autopa){ playAudio(at); autopa=false; } }) $(".audio-btn").on("click",function(){ if(audobtn){ $(".audio-btn").removeClass("move1"); x.pause(); audobtn=false; }else{ $(".audio-btn").addClass("move1"); x.play(); audobtn=true; } })
It requires the introduction of micro-channel and jq JS <Script the src = " ./jquery-1.7.2.min.js " type = " text / JavaScript " > </ Script> <Script the src = " ./jweixin-1.0.0.js " > </ Script> <Script> function autoPlayMusic () { // automatically play sound effects, to solve the browser or APP Autoplay function musicInBrowserHandler () { musicPlay ( to true ); document.body.removeEventListener ( ' touchstart ' , musicInBrowserHandler ); } document.body.addEventListener ( 'touchstart', MusicInBrowserHandler); // automatically play sound effects, to solve the problem of micro-channel auto-play function musicInWeixinHandler () { musicPlay ( to true ); document.addEventListener ( " WeixinJSBridgeReady " , function () { musicPlay ( to true ); }, to false ); Document. the removeEventListener ( ' the DOMContentLoaded ' , musicInWeixinHandler); } document.addEventListener ( ' the DOMContentLoaded ' , musicInWeixinHandler); } function musicPlay (DisplayRID) { var audio = document.getElementById('audio2'); if (isPlay && audio.paused) { audio.play(); } if (!isPlay && !audio.paused) { audio.pause(); } }; autoPlayMusic(); // 关闭音乐 $('.music-pic').click(function () { if($('.music-pic').hasClass('close')){ document.getElementById("audio2").muted=false; $('.music-pic').removeClass('close'); }else { $('.music-pic').addClass('close'); document.getElementById("audio2").muted=true; } });
Reproduced in: https: //www.cnblogs.com/qdlhj/p/10749991.html