RomMer
RomMer

Reputation: 1089

Javascript pause audio playing after x seconds

I first defined a start time for audio playing and an end time both in seconds

var a = document.getElementById("audioplayer");
a.addEventListener("progress", function () {
    if ($("#audioplayer")[0].currentTime > Math.round(Number(globalStart) + Number(globalDuration)))  {
        $("#audioplayer")[0].pause();
    }
}, true);

I want to stop audio playing when current time reaches x seconds

how can I do that?

Upvotes: 0

Views: 590

Answers (1)

Alex Slipknot
Alex Slipknot

Reputation: 2533

You have to add event listener on timeupdate

a.addEventListener('timeupdate', function () {
    if (this.currentTime > Math.round(Number(globalStart) + Number(globalDuration)) && !this.paused)  {
        this.pause();
    }
});

Upvotes: 2

Related Questions