$(document).ready(function () { var audioElement = $( '<audio>' + ' <source src="" />' + '</audio>'); $('body').append(audioElement); var a = audioElement[0]; var lastEventTime = null; var queue = []; function play() { //如果队列是空的,则直接返回 if (queue.length == 0) return; if (a.readyState == 0 || a.ended) { var event = queue.pop(); a.src = "xxxxxxxxxxxx";//这儿报DOMException: The play() request was interrupted by a call to pause(). 的问题. a.play(); } }
后改为:
$(document).ready(function () { var audioElement = $( '<audio>' + ' <source src="" />' + '</audio>'); $('body').append(audioElement); var a = audioElement[0]; var lastEventTime = null; var queue = []; function play() { //如果队列是空的,则直接返回 if (queue.length == 0) return; if (a.readyState == 0 || a.ended) { try { a.pause(); a.currentTime = 0; //解决在chrome 控制台中报 DOMException: The play() request was interrupted by a call to pause(). 的问题. //这是第1种方式 //var nopromise = { // catch: new Function() //}; // (a.play() || nopromise).catch(function () { console.log("a.play catch>"); });;//这种可以 var event = queue.pop(); a.src = "xxxx"; //这是第2种方法 a.play().catch(function (e) { // console.log("a.play catch>", e); }); } catch(e) { console.log("autoplay.js play()>", e.message); console.log("autoplay.js play()>", e.description); } } }
这样就可以解决问题了.