chrome播放語音時,在chrome 控制檯中報 DOMException: The play() request was interrupted by a call to pause(). 的問題

$(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();
}
}

後改成:chrome

$(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);
            }

        }
    }

 

這樣就能夠解決問題了.promise

相關文章
相關標籤/搜索