Search code examples
javascriptaudiorandomtimeplayback

How to play a song from a random time in javascript ? (Ex:from the 5th second)


I'm trying to play a song from a specific time code. This, document.getElementById('rock').play(); play the song from the beginning. Is there a way to choose the beginnning time? Thank you.

document.getElementById('rock').play();


Solution

  • The HTMLMediaElement.currentTime property gives the current playback time in seconds. Setting this value seeks the media to the new time.

    HTMLMediaElement.currentTime

    let song = document.getElementById('rock');
    song.currentTime = 35;