Setting the currentTime() in Video.js doesn't seem to be working

90 views Asked by At

I'm trying to implement video player that remembers where user has left watching video, using video.js player. Player is saving current video time into local storage, and when user returns to the video, it sets that time, so users could continue where they left. And sometimes player method currentTime(time) doesn't work.

Here is my code:

function _(el) {
    return document.getElementById(el);
}

const player = videojs("video-id")
let videoId = undefined

function playVideo() {
    const textElement = _("text-id")
    videoId = textElement.value
    if (!player.paused) {
        player.pause()
    }
    player.reset()
    player.src({type: 'video/mp4', src: `/download/${videoId}`})
    player.load()
    const leftTime = localStorage.getItem(videoId)
    console.log("Before set:", player.currentTime()) // 0 (int)
    console.log("Left time:", leftTime) // 173 (string)
    if (leftTime !== null) {
        const leftTimeInt = parseInt(leftTime)
        console.log("Left time int: ", leftTimeInt) // 173 (int)
        player.currentTime(leftTimeInt)
        console.log("After set:", player.currentTime()) // 0 (int)
    }
    console.log("New video time: ", player.currentTime()) // 0 (int)
}

I expect it to log "After set: 173", but it doesn't happen. And Video.js documentation only says that currentTime() allows to "Get or set the current time (in seconds)".

0

There are 0 answers