We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm loading several audio files with loadSound() and storing them in an array, and some of the files can't be found (404 error). However, this is a normal behavior or my sketch (I'm parsing external data and using it to load the files, so there might be inconsistencies), and I wanted to know if I could catch these errors and simply ignore them.
var list = ["horn", "bell", "guitar"];
var allSounds = [];
function preload() {
for (i=0; i<list.length; i++) {
var path = "audio/" + list[i] + ".mp3";
var thisSound = loadSound(path, storeSound);
}
}
function storeSound(soundFile) {
// could I catch the errors here?
allSounds.push(soundFile);
}
Answers
insert this
and this
instead of all sounds as an array you could make it an ArrayList (to avoid empty slots in the array)
or with an array you can probably check whether the entry is != null and only then play it
@Chrisir: did you test that in p5js? It looks like Java code and not JS :/
When I try and load a file from an invalid URL loadSound simply chokes. Looking at the source there doesn't appear to be any obvious error handling for a failed load; which is a shame as it should be fairly simple to check the request status and fail gracefully...
As such the best thing @ncc1989 can do is file a bug report.
Also this is p5js: JavaScript arrays are not fixed like Java arrays: they're as flexible as ArrayLists...
Ideally what should happen is that loadSound fires a callback on success or failure - including a reference to the sound file if loaded. You could then append to your array based on that status:
[edit: refactored my suggested code; but I think it needs more thought...]
Right on the spot: None whatsoever! 3:-O
Take a look below... just onload() and no onerror() to be seen: 8-}
IMO, that's a serious gross error! Why would a developer assume that's OK not to provide an error handler callback for resource loading??? =;
And instead, provide some superfluous whileLoading() callback??? :-@
I can't see another way but to hack and override p5.SoundFile.prototype.load() till they come on top of their senses! /:)
Actually the only Java container class that comes as close as JS' is the slowest of all LinkedList:
http://docs.Oracle.com/javase/8/docs/api/java/util/LinkedList.html
That's why JS' arrays are much slower than Java's. In order to provide such flexibility, performance sacrifices needs to be made... :-q
Thanks to all for the replies! Agreed with @blindfish and @GoToLoop, there really should be an error handler for loadSound().
@Chrisir 's idea of checking if the file exists can be applied, but the way I did it is pretty much a hack. I used jQuery's $.get to check if the file exists. This is a bad approach because it adds another library and makes another asynchronous request. In any case, I'll paste it here for those who don't want to hack the p5 library directly!
@ncc1989 - you can indeed apply Chrisir's suggestion, but there's a better library to use than jQuery. It's called vanillajs :P
Seriously though, these days a simple request like this isn't too difficult to implement without a library and it's a useful learning exercise. And do submit an issue to the p5 sound team: they seem pretty receptive and responsive...
@blindfish – agreed, using vanilla js would definitely be quicker, but the syntax for this kind of request drives me so kind of crazy that I even forget sometimes it is possible :p
But thanks for the tip! I've just opened an issue for the missing error handling here: https://github.com/processing/p5.js-sound/issues/83
Just to let you all know that the issue was closed: https://github.com/processing/p5.js-sound/issues/83
Here's the new loadSound() function, as updated by @therewasaguy:
loadSound(path, [on load callback], [on file not found callback], [while loading callback]);