Given is a simple clean HTML5 audio player. To make the time duration work, the preload="none"
must be left out in this code, which defeats the whole purpose of having a minialistic audio player!
What I want is to: wait and don't load the mp3 file (saving many megabytes of otherwise wasted data) unless and untill a user clicks on PLAY! Then and only then, as the mp3 loads and plays, show the duration information. How to achieve this?
In other words: as long as nobody has clicked the PLAY, no data should be loaded from the server and duration is simply 00:00. Thanks!
/* BAD: Loads mp3 file, even of nobody clicks play!
GOOD: Shows duration time correctly. */
<audio src="http://labs.qnimate.com/files/mp3.mp3" type="audio/mpeg"></audio>
/* BAD: Doesn't show the duration time!
GOOD: Doesnt load the mp3 file unless user clicks play. */
<audio src="http://labs.qnimate.com/files/mp3.mp3" type="audio/mpeg" preload="none"></audio>
https://jsfiddle.net/xt5c0whx/10/
The different states summed up for clarity:
• Webpage loads, but the 10 megabyte mp3 does NOT load, yet. Duration displays as "00:00"
• If user clicks play, then mp3 is loaded, played and duration shows the audio length time
click
handler. When some click on it, load the audio file with usingnew Audio()
. So after that you can keep in memory with assigning to a variable. See more for details: developer.mozilla.org/tr/docs/Web/API/HTMLAudioElement – ZaphielmyAudio.duration
in your onclick/play event listener function? – NewToJS