2016-04-12 183 views
0

我有一个基于Web的消息系统,它在桌面上很好用,但声音不在移动浏览器上启动。 的指令如下:音频不播放移动浏览器

var mySound = new buzz.sound("/sounds/new_msg", { 
             formats: [ "ogg", "mp3", "aac" ] 
            }); 

            mySound.play(); 
           }, function (err) { 
            // code to handle read error 
            console.log(err); 
           }); 

回答

0

确实这个问题,会出现什么浏览器?

尝试使用此代码,更新我们对结果:

function initAudio() { 
    var audio = new Audio('./path/to/my/sound.mp3'); 
    audio.addEventListener('play', function() { 
     // When the audio is ready to play, immediately pause. 
     audio.pause(); 
     audio.removeEventListener('play', arguments.callee, false); 
    }, false); 
    document.addEventListener('click', function() { 
     // Start playing audio when the user clicks anywhere on the page, 
     // to force Mobile Safari to load the audio. 
     document.removeEventListener('click', arguments.callee, false); 
     audio.play(); 
    }, false); 
} 
相关问题