Loading sounds on the fly

A little addition to the approach above, since I’ll point @Zhaleh to this thread. A nice way of checking whether a resource has been downloaded is as followed:

Step 1. Have a routine for downloading resources, maybe add a text like “One moment please” to keep your participant informed.

Step 2. Add a code component.
Step 2a. In the begin routine tab of that code component, call PsychoJS to download the resource. For example:

psychoJS.downloadResources([
  { name: 'my_sound.mp3', path: 'sounds/my_sound.mp3' }
]);

Step 2b. In the each frame tab, keep checking whether the sound has downloaded. Once it does, finish the routine, and you can use the sound in a successive routine. Below we use the following trick: if psychoJS.serverManager.getResource('my_sound.mp3') does not throw an error, the resource is available.

try {
    psychoJS.serverManager.getResource('my_sound.mp3');
    continueRoutine = false;
} catch (e) {}

The example above is an adaptation of: Thomas Pronk / demo_font · GitLab

1 Like