Hello, I am interested in the question: how can I make the played music not start playing again when I restart (the time to stop was saved (as with the video on youtube)) I was advised to use the onunload () event, but I don’t know how to implement this. Can you show how to do?
1 answer
You can use localStorage to store the currentTime audio track. And on the onunload () event, update this time ( here the example is not played, access to localStorage is denied - copy the code to your local page) :
var pl = document.getElementById('player'); var playTime = localStorage.getItem('time'); //получаем значение времени трека из локального хранилища if(playTime == null) { //если значение не установлено... pl.currentTime = 0.0; //запускаем трек с самого начала } else { //если какое-то значение есть... pl.currentTime = playTime; //перемещаем бегунок на эту позицию... } pl.play(); //...и запускаем воспроизведение window.onunload = function(){ //в случае закрытия или перезагрузки страницы localStorage.setItem('time', pl.currentTime); //записываем в хранилище время трека, на котором прервались } <audio id="player" src="http://files.realmusic.ru/download/1577511/id195399_-_bi-2_feat._Oxxxymiron_-_pora_vozvraschatsya_domoi_(lyric_video).mp3" controls></audio> |