Preload the next song in the playlist before the current one ends

I made a small media player that works great, but I want to make it so that there is no more download between each song

I know about the preload property, but it only preloads the music when the page is loaded for the first time, so I feel like this is not working

Is there a way to do this at all? perhaps using the web audio API?

+3


source to share


1 answer


When you start playing a song, you can watch the play

audio event and already start preloading the next song in the queue.

This is the function I use to preload audio, you can use it anytime, not only the first time the page is loaded:



function preloadAudio (filename) {

    var sound = new Audio();
    sound.preload = 'auto';

    sound.addEventListener('canplaythrough', function () {
        // now the audio is ready to play through
    });

    document.body.appendChild(sound);

    sound.src = filename;
    sound.load();

}

      

+3


source







All Articles