I am creating a web application that can be opened for a long time. I donโt want to download audio during loading (when HTML is being loaded and parsed) in order to make the first download as fast as possible and save precious resources from mobile users. Audio is muted by default. Putting audio in CSS or using preloading is not suitable here because I donโt want to load it while loading with the rest.
I'm looking for the perfect method for loading audio at runtime , (after the checkbox is checked, it can be 20 minutes after the application opens), given the list of audio elements.
The list is already in the allSounds variable. I have the following sound on a webpage (there is more):
<audio preload="none"> <source src="sound1.mp3"> </audio>
I want to keep the same HTML, because after the second visit I can easily change it (this works fine with server-side HTML generation)
<audio preload="auto"> <source src="sound1.mp3"> </audio>
and it works.
Once the option is enabled, I want to download sounds, but not immediately play them. I know .play () loads sounds. But I want to avoid the delay between pressing a button and the corresponding feedback sound. Better not to play sound than to linger (in my application).
I made this event handler for loading sounds (it works), but in the chrome console it says that the download was canceled and then restarted. I donโt know exactly what I am doing wrong. Is this the right way to make load sounds? What are other ways? If possible, without changing the HTML.
let loadSounds = function () { allSounds.forEach(function (sound) { sound.preload = "auto"; sound.load(); }); loadSounds = function () {};
here is the playSound function, but not very important for questions
const playSound = function (sound) { // PS /* only plays ready sound to avoid delays between fetching*/ if (!soundEnabled)) { return; } if (sound.readyState < sound.HAVE_ENOUGH_DATA) { return; } sound.play(); };
Side question: should there be preload="full" in the HTML specification?
See also: Preload the mp3 file in the queue to avoid delays in playing the next file in the queue
how can we play audio with text highlighted word to word angularjs