7-20 9,321 views
苹果微信会拦截视频音频的自动播放,导致play()事件不生效,移动网页开发者可能会遇到或没发现到这问题,网页时不时需要自动播放背景音乐,应使用如下代码解决
- var myaudio=document.getElementById("myaudio");
- myaudio.volume=1;
- myaudio.play();
- audioPlay(myaudio)
- //移动端自动播放音频
- function audioPlay(ele){
- if(window.WeixinJSBridge){
- WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
- ele.play();
- }, false);
- }else{
- document.addEventListener("WeixinJSBridgeReady", function() {
- WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
- ele.play();
- });
- }, false);
- }
- }