TypeError: win is undefined when playing audio with custom function

URL of experiment: https://gitlab.pavlovia.org/kqlu4156/learning-alternations

Description of the problem: My project includes some custom code to play an audio clip when the participant clicks on a speaker. It currently crashes after clicking on the speaker with the error message TypeError: win is undefined

Console locates the error in the Sound constructor:

Class Sound extends PsychObject
{
	constructor({
								name,
								win,
								value = 'C',
								octave = 4,
								secs = 0.5,
								startTime = 0,
								stopTime = -1,
								stereo = true,
								volume = 1.0,
								loops = 0,
								autoLog = true
							} = {})
	{
		super(win._psychoJS, name);
        (etc.)

The line win=psychoJS.window; does appear in the experimentInit() function in my generated js code, so win should be defined.

For reference here is the custom code I am using to play the sound:

In Begin Routine:

play_exposure_sound = function() {
    var exposure_sound;
    exposure_sound = new sound.Sound({"value": exposure_file_name, "volume": 1.0});
    exposure_sound.play();
    core.wait(0.3);
}

In Every Frame:

if (exposure_mouse.isPressedIn(exposure_speaker, {"buttons": [0]})) {
    play_exposure_sound();
}

Thank you in advance!

Thanks for a lovely bug report! I guess you need to provide the win argument to the sound constructor. Maybe something like this?

new sound.Sound(
  {"value": exposure_file_name, "volume": 1.0},
  psychoJS.window
);
1 Like

This was it, thank you very much!

For anyone else having this issue, the exact way I had to provide the argument ended up looking like this:

new sound.Sound(
  {"win": psychoJS.window, "value": exposure_file_name, "volume": 1.0}
);
1 Like