N
N
Nikita Podosenov2020-04-30 19:14:17
JavaScript
Nikita Podosenov, 2020-04-30 19:14:17

Why does this error appear on audio.play()?

When the page loads, audio.play() is called. I get the error "Uncaught (in promise) DOMException: The play() request was interrupted by a call to pause().", and nothing works. The bottom line is that I'm still finalizing the code and this error did not exist before. Is there any way to fix this?

function create() { 
        work = true;
        audio = new Audio(playlist[j]);
        progressOn();
        timerOn();
        activeTrack(j);
}

function playPause(clicked = false) {
    if (work) {
        try {
            if (pp) {
                audio.play();
                document.getElementById('play').src = 'pause.png';
                move = true;
                clearInterval(interval);
                interval = setInterval(moving, 1000);
                pp = false;
            } else {
                audio.pause();
                document.getElementById('play').src = 'play.png';
                if (i == 0) {
                    audio.play;
                    i++;
                }
                clearInterval(interval);
                pp = true;
                move = false;
            }
        } catch {
            audio.pause();
            move = false;
        }
    } else if (clicked) {
        j = 0;
        work = true;
        create();
        playPause();
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yaroslav Ivanov, 2020-04-30
@space2pacman

Because browsers prohibit playing audio without user action.
https://developers.google.com/web/updates/2017/06/...
Solution: execute the function when the button is clicked.

X
xmoonlight, 2020-04-30
@xmoonlight

Here is your code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question