2

I am trying to preload audio files via JavaScript and the HTML Audio API.

Safari on iOS (aka safari mobile) only allows .load() on "user interaction". Now I am wondering what that means exactly.

For example: I trigger the code described in the top answer of

Preload multiple audio files :

var audioFiles = [
    "http://www.teanglann.ie/CanC/nua.mp3",
    "http://www.teanglann.ie/CanC/ag.mp3",
    "http://www.teanglann.ie/CanC/dul.mp3",
    "http://www.teanglann.ie/CanC/freisin.mp3"
];
    
function preloadAudio(url) {
    var audio = new Audio();
    // once this file loads, it will call loadedAudio()
    // the file will be kept by the browser as cache
    audio.addEventListener('canplaythrough', loadedAudio, false);
    audio.src = url;
}
    
var loaded = 0;
function loadedAudio() {
    // this will be called every time an audio file is loaded
    // we keep track of the loaded files vs the requested files
    loaded++;
    if (loaded == audioFiles.length){
     // all have loaded
     init();
    }
}
    
var player = document.getElementById('player');
function play(index) {
    player.src = audioFiles[index];
    player.play();
}
    
function init() {
    // do your stuff here, audio has been loaded
    // for example, play all files one after the other
    var i = 0;
    // once the player ends, play the next one
    player.onended = function() {
     i++;
        if (i >= audioFiles.length) {
            // end 
            return;
        }
     play(i);
    };
    // play the first file
    play(i);
}
    
// we start preloading all the audio files
for (var i in audioFiles) {
    preloadAudio(audioFiles[i]);
}
<audio id="player"></audio>

on a button click but still have issues with files not being cached...

Does the for loop break the user interaction? How many steps/function calls can I do in the background until a load() is not considered an "user interaction" anymore?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
gaugau
  • 765
  • 1
  • 9
  • 30
  • Thank you, Luca, for the corrections. Embarrassing style, i must admit. Very much appreciated – gaugau Jun 26 '18 at 15:51

0 Answers0