var voice = './sound/14t888piCqei.mp3'; var voice = new Audio(voice); voice.play(); //播放
chrome >=66 不支持音频自动播放
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
降级处理不播放
function soundPlay () { // 返回一个promise以告诉调用者检测结果 return new Promise(resolve => { let audio = document.createElement('audio'); audio.src = '../dist/sound/14t888piCqei.mp3'; document.body.appendChild(audio); let autoplay = true; // play返回的是一个promise audio.play().then(() => { // 支持自动播放 autoplay = true; }).catch(err => { // 不支持自动播放 autoplay = false; }).finally(() => { // 告诉调用者结果 resolve(autoplay); }); }); }
WebSocket轮询消息,提示声音,git:https://github.com/vagsy/websocket
参考: