Javascript :: Is audio.pause reset audio.currentTime to 0?

My code works fine except that instead of pausing the track, it goes back to the beginning of the track.

Here is my code:

var audio=new Audio();

var hezi=document.querySelector('#player>.button.hand');
var gangina=("textContent" in document)?"textContent":"innerText";

hezi.addEventListener
(
    'click',
    function()
    {

        if(audio.paused)
        {
            audio.src="Nadav Guedj - Golden Boy (Eurovision 2015 - Israel - Karaoke).mp3";
            hezi.innerHTML="►";
            audio.play();
        }
        else
        {
            hezi.innerHTML="||";
            audio.pause();
        }

    }
);

      

I'm going to assume that in order to solve this problem, I need to store the value of audio.currentTime

this track with window.setInterval

and use the last value audio.currentTime

to play the track from the last point where it stopped, but I was wondering if there is a way to make the pause / play function correct (not to mention, if I saved that last value through window.setInterval

, it wouldn't be 100% accurate, as I rely on the spacing value, not the exact specific point at which the user stopped the track).

+3


source to share


2 answers


it goes back to the beginning because of this line,

        audio.src="Nadav Guedj - Golden Boy (Eurovision 2015 - Israel - Karaoke).mp3";

      



remove that, then audio.play will resume where you left off. demo script

+2


source


Try storing it in the currentTime variable in your click event handler when the user pauses the player.



This way you will have the exact moment the music stops, just store it on a variable from the object's scope.

+1


source







All Articles