scottschiller / SoundManager2

A JavaScript Sound API supporting MP3, MPEG4 and HTML5 audio + RTMP, providing reliable cross-browser/platform audio control in as little as 12 KB. BSD licensed.
http://www.schillmania.com/projects/soundmanager2/
Other
4.99k stars 768 forks source link

Decreasing initial time to play while loading. #163

Closed websanova closed 7 years ago

websanova commented 7 years ago

Hey, great little library, works like a charm so far.

I'm just wondering though. it seems to always load the song at about 28% when I check bytesLoaded. inside of whileloading. Before that it just shows 0 constantly. Is there anyway to get data before that or is it just a browser/html5 thing?

scottschiller commented 7 years ago

Howdy!

With HTML5, the UA is pretty much entirely in control of the HTTP request/response sequence and amount/order of bytes downloaded. You can specify preload and similar on the native HTML5 <audio> or Audio() objects, but it makes little difference from what I've found and is N/A on mobile devices. By default, HTML5 clients will request partials/byte ranges if the server supports them.

In effect, the only case where a client will "pre-load" 100% of the file is if you were to request it via XHR or similar other "progressive" single-request download beforehand - which is these days considered an anti-pattern anyhow, since the point of HTML5 is to be efficient and allow users to seek arbitrarily within audio files that may be many hours long. (Consider video, i.e., seeking on YouTube; same sort of argument, even though audio is of course a lower bitrate.)

In SM2's case, I fire "onload" and friends when the UA indicates the canplay event or similar via HTML5. There is also canplaythrough, which is the UA's best guess at "I can play this sound completely from this point onward, without having to stop playback to buffer."

A little more here on how HTML5 clients request audio. http://www.schillmania.com/projects/soundmanager2/doc/technotes/#client-requests

scottschiller commented 7 years ago

I should add, feel free to reopen this if you've found a bug, etc. ;)