1

If I have a hyperlink that links to an mp3 file on another server, how can I have the browser only download the first 15 seconds (or some other duration) of that file when a user clicks on the link? Is there an HTML5 attribute that will do this automatically?

Nona
  • 5,302
  • 7
  • 41
  • 79
  • 1
    IMO, _Impossible_.. Refer [_partially download a file with Javascript_](http://stackoverflow.com/questions/13704615/partially-download-a-file-with-javascript) – Rayon Jun 02 '16 at 04:20

1 Answers1

4

Yes, sort of - there is the Media Fragment URI specification which allows you to specify time ranges in the URI. You can append an URI with a fragment and t=:

#t=from,to

For example:

http://domain.to/music.mp3#t=0,10    (or just t=,10 in this case if from=0)

will load data from 0-10 seconds.

Example

Loading... will 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 this link:<br>
<a href="https://mp3l.jamendo.com/?trackid=912259&format=mp31#t=0,10">
CTRL + Click link to play 10s of audio in a new tab...</a>

However, this won't necessarily prevent the browser from loading the rest of the audio data. The fragment just tells the media element to start and stop at certain times without the need to do so manually beyond defining the fragment time.

Setting preload to none may help, but the browser can load the complete file even then. You can dig down into Media Source Extensions to control how the buffer loads and caches the data, but it's a wide topic.

Or load via XHR using the Range header combined with the fragment uri, but you would need to know a range of details about the audio file in advance to figure out the byte range such as bit rate, channels etc.