HTML 5 <audio> - Play file at certain time point

前端 未结 2 1527
日久生厌
日久生厌 2020-12-01 08:08

I have a simple auto playing snippet that plays the audio file however I was wondering either in JavaScript or as an attribute play that file at a certain time (ex. 3:26).

2条回答
  •  既然无缘
    2020-12-01 08:35

    The best way to do this is to use the Media Fragment URI specification. Using your example, suppose you want to load the audio starting at 3:26 in.

    
    

    Alternatively, we could just use the number of seconds, like file.mp3#t=206.

    You can also set an end time by separating the start from the end times with a comma. file.mp3#t=206,300.5

    This method is better than the JavaScript method, as you're hinting to the browser that you only want to load from a certain timestamp. Depending on the file format and server support for ranged requests, it's possible for the browser to download only the data required for playback.

    See also:

    • MDN Documentation - Specifying playback range
    • W3C Media Fragments URI

提交回复
热议问题