Sounds set in code do not play on Pavlovia

Hi, I have a PsychoPy Builder exp turned into a PsychoJS to run on Pavlovia (it runs runs locally as it should).
In the main trial routine(s) there are multiple image and two audio components (audio_1 and audio_2).
Audio_1: Start: 0.01 (so basically cc. when all the images appear), Stop - condition: $audio1._isFinished;
Audio_2 is played after audio_1 finishes.
The two sounds are set to variables (changing every repeat), such as audio2_path.
The exact content is defined in the prior, fixcross routine (e.g. audio2.setSound(audio2_path), essentially referring to a condition file. Both sounds are created earlier, in another routine e.g.
start_audio = “audios/bouger.mp3”
audio1 = new sound.Sound({
win: psychoJS.window,
value: start_audio
});
main_audio = “audios/silence.mp3”;
audio2 = new sound.Sound({
win: psychoJS.window,
value:main_audio
});

The main issue is that the sounds are not played in the online version. Audio1 is not played at all, Audio2 may be played once in the third trial or so and that is it, I hear some clicking, so the silence.mp3 may be played from time to time.
The rources are loaded at the beginning.

I read a few threads here including the cribsheet and this: Asynchronous sound success story, where there is a potential solution for such problems.
However, I don’t see how I could adapt this solution to my case.
Beside having two sounds and not just one, I also have a prior rountine (fixcross routine) the duration of which is not fixed, as i nthe example by wakecarter. This prior rountine lasts max is 2 sec but participants have to click on a button presented in this rountine - to ensure a certain starting position of the mouse in the main_trial routine). So duration can be anything.
I would appreciate any help.

Please could you clarify the stop condition for audio 1? I’ve just added a second audio to my sound test

sound_2 start condition is sound_1.status == FINISHED (start time .1 seconds)

Timeout in routine settings is sound_2.status == FINISHED

These are just sound components with no code. However, I also have a replay sound online demo which plays two sounds repeatedly in code.

Replay | code | try it | discussion thread

Two audio files are loaded into two sound components during the ITI, but played with zero volume. During the trial routine, buttons are used to play the sound files. Once both audio files have been selected at least once, the trial can be ended with a forced choice. In this demo the pairs are fixed, but independent randomisation could be used if desired.

I never create sound objects in code.

Thank you for the suggestions and the examples. The sound2 Stop condition (in Builder) is: $audio1._isFinished. It is important that both audios are fully played even if there is a response while they are played (not that likely in case of audio1, but anything can happen). Btw: In the meantime I managed to solve the issue. What I did was based on your suggestions with a little twist:

  • I "moved’ the audio 1 sound component in Builder to the previous routine (the fixcross rountine that ends w a response), and set the start value to a boolean var
    that turns true when there is a response / trialtime is over (checked every frame). With this boolean turning true, the fixcross routine stops. The sound starts to play with the end rountine (if audio1_canstart === true…) and is stopped in the next rountine’s code component where I check whether its duration + a necessary lag is over. This solved the problem for audio 1 but not yet for audio2.
  • The audio2 component finally stayed in the main trial rountine (where I present images and partiicpants make a response, based o nthe audio2 part and the images). The start condition is a variable that turns 1 after the the audio1 (+the lag) is over. I had to include audio2.play() in this code component to make things work.
    Every frame:
    if (((instr_start_over === 0) && (t>instr_start_dur))) {
    audio1.stop();
    if (!instructionTimerStarted) {
    instructionTimerStarted = true;
    instructionStartTime = timer.getTime();
    }
    if ((timer.getTime() - instructionStartTime) > 0.1) {
    instr_start_over = 1;
    timer.reset();
    audio2.play();
    }
  • I had to define sounds in the code with the above described stop conditions simply because when I only had the componets in the Builder, with a variable (e.g. $audio2_path) in the Sound row (content defined i nthe fixcross code component), no audio was played/a beep only i nthe main rountine.

My current solution may not be ideal, so I will look deeper into the ones you suggest now. But it seems to work and allows me to go on to deal with other issues (images not appearing in their original size, being blurry etc.). Thank you for the time and effort you invested!