问题
My code is working perfectly except the fact that instead of pause the track it's going 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 to solve this issue I need to store the audio.currentTime value of that track with window.setInterval and use the last audio.currentTime value to play the track from the last point where it stopped but I was wonder if there is continent way to make the pause/play button function properly (not to mention the fact that if I would store that last value via window.setInterval it wouldn't be 100% accurate since I'm relying on the interval value and not the exact specific point where the user paused the track).
回答1:
it is going back to the beginning because of this line,
audio.src="Nadav Guedj - Golden Boy (Eurovision 2015 - Israel - Karaoke).mp3";
remove that, then audio.play would continue from where you paused. fiddle demo
回答2:
Try to store in a variable the currentTime on the click event handler when user pauses the player.
This way you'll have the exact moment where the music was stopped, just store it on a variable out of object's scope.
来源:https://stackoverflow.com/questions/29918850/javascript-is-audio-pause-reset-audio-currenttime-value-to-0