javascript - How to setup a hyperlink that only downloads part of a file? -
if have hyperlink links mp3 file on server, how can have browser download first 15 seconds (or other duration) of file when user clicks on link? there html5 attribute automatically?
yes, sort of - there media fragment uri specification allows specify time ranges in uri. can append uri fragment , t=
:
#t=from,to
for example:
http://domain.to/music.mp3#t=0,10 (or t=,10 in case if from=0)
will load data 0-10 seconds.
example
loading... auto-play<br> <audio src="https://mp3l.jamendo.com/?trackid=912259&format=mp31#t=0,10" autoplay controls preload="none"></audio> <br><br>or click link:<br> <a href="https://mp3l.jamendo.com/?trackid=912259&format=mp31#t=0,10"> ctrl + click link play 10s of audio in new tab...</a>
however, won't prevent browser loading rest of audio data. fragment tells media element start , stop @ times without need manually beyond defining fragment time.
setting preload none may help, browser can load complete file then. can dig down media source extensions control how buffer loads , caches data, it's wide topic.
or load via xhr using range header combined fragment uri, need know range of details audio file in advance figure out byte range such bit rate, channels etc.