0

I want to preload multiple audio files. To do this, I tried to create multiple Audio elements in JavaScript.

function loadAudio(){
    audio1 = new Audio();
    audio1.addEventListener('canplaythrough', isLoaded, false);
    audio1.src = 'assets/audio/Maid with the Flaxen Hair.mp3';
    audio1.load();
}

function isLoaded(){
    audio1.removeEventListener('canplaythrough', isAppLoaded);
    alert('maid');

alert('start audio 2');
audio2 = new Audio();
audio2.addEventListener('canplaythrough', isLoaded2, false);
audio2.src = 'assets/audio/Kalimba.mp3';
audio2.load();
}

function isLoaded2(){
    alert('kalimba');
}

I only get the first alert, the second one never works.

I found that I can only play one sound at a time, but can I also only load one? Does the script need another user input for every new Audio object I create? Or does anyone have another way to create a preloader for audio?

Kris
  • 824
  • 2
  • 12
  • 28

2 Answers2

0

this could have to do with the limitations on autoplaying (and, as far as i know autoloading) audiofiles on ios-devices (see here: Limitations of HTML5 Audio on iOS 4?).

In short: You cannot programmatically start audio-playback on ios devices, this is only allowed from within event-handlers for trusted (=initiated by the user) events.

Community
  • 1
  • 1
Martin Schuhfuß
  • 6,814
  • 1
  • 36
  • 44
0

Yo can play multiple audio on IOS device using following way

    $("#btn").on("click",function(){
                        alert("audio clicked");
                        var aud=new Audio();
                        aud.src="music.mp3";
                        aud.play();
                        var aud1=new Audio();
                        aud1.src="intro.mp3";
                        aud1.play();
                    }) 

The above code play multiple audio on device

pareshm
  • 4,874
  • 5
  • 35
  • 53