# Is it possible to preload sounds into an array with p5 sound?

**URL:** https://discourse.processing.org/t/is-it-possible-to-preload-sounds-into-an-array-with-p5-sound/25306
**Category:** Libraries
**Created:** [November 9, 2020, 7:51pm UTC](https://discourse.processing.org/t/is-it-possible-to-preload-sounds-into-an-array-with-p5-sound/25306 "2020-11-09T19:51:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![egg\_yolk](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@egg\_yolk](https://discourse.processing.org/u/egg_yolk)
#### Post date: [November 9, 2020, 7:51pm UTC](https://discourse.processing.org/t/is-it-possible-to-preload-sounds-into-an-array-with-p5-sound/25306/1 "2020-11-09T19:51:37Z")

</div>

I am using p5.js in a rather unconventional way where having sounds in an array would help a lot. Is it possible to do something like,

```auto
function preload(){
	for(i = 0; i < songLocations.length; i++){
		p5SongList[i] = loadSound(songLocations[i])
	}
}

```

or is this the only current way to load sounds,

```auto
function preload(){
	sound = loadSound(songList[0]);
	sound1 = loadSound(songList[1]);
	sound2 = loadSound(songList[2]);
	sound3 = loadSound(songList[3]);
	sound4 = loadSound(songList[4]);
}

```

Thanks!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 9, 2020, 7:59pm UTC](https://discourse.processing.org/t/is-it-possible-to-preload-sounds-into-an-array-with-p5-sound/25306/2 "2020-11-09T19:59:48Z")

</div>

> [@egg\_yolk](#):
>
> Is it possible to do something like,

Yes it is! Haven’t you tried that out yet? 😐

---

<div class="post-metadata">

### Author: ![egg\_yolk](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@egg\_yolk](https://discourse.processing.org/u/egg_yolk)
#### Post date: [November 9, 2020, 10:13pm UTC](https://discourse.processing.org/t/is-it-possible-to-preload-sounds-into-an-array-with-p5-sound/25306/3 "2020-11-09T22:13:35Z")

</div>

I did try this, but p5SongList[0] was undefined… I forgot to initiate the array in global scope 🙃

The below does indeed work!

```auto
var p5SongList = []

function preload(){
	for(i = 0; i < songLocations.length; i++){
		p5SongList[i] = loadSound(songLocations[i])
	}
}

function draw(){
	if(!p5SongList[0].isPlaying()){
		p5SongList[0].play();
	}
}

```
