Audio file doesn't stop after keypress
Hello!
I want participants in my experiment to listen to a long-ish audio file so they can adjust the audio volume to a comfortable level. I would like the file to stop playing once they hit a key, and then have them move on to the experiment. I tried setting the duration of the sampler to keypress, but that didn't work as expected, the file continued to play. Is there another way to achieve this or a better way of allowing them to test and adjust the volume? I can't use Python as I'm planning to run the experiment online.
Thanks in advance!
Comments
Hi @sflu,
Indeed, the sampler's keypress or mouseclick end event is not operational when you run the experiment in the browser. This makes it a little tricky to stop a actively sound, but I could think of at least one way around that problem. It does require that you store your sound file on a server that allows cross-origin resources sharing (CORS). Github does so, so you could create an account there (if you don't already have one), upload your long sound file there, and then identify the URL to the raw file.
Once you have that set up, you can use some Javascript code to load up the sound, start it and stop it whenever you want (for example when the subject presses a key).
Pay special attention to placing the code loading up the sound under the prepare tab of the inline_javascrpt:
Code:
vars.mysound = new Audio('https://www.bensound.com/bensound-music/bensound-moose.mp3')You'd have to replace the URL by that corresponding to your sound.
Then, to play the sound:
And to stop it:
If you only need to play the long sound at the beginning so that the subjects adjust their sound level, that should work. You can then use the sampler for the shorter sounds you may use in your task trials.
Another option would be to provide your participants with a link to the sound file and ask them to download it, play it, adjust the sound level, and then to click on a separate link where you'd have your experiment. Just another way for them to calibrate the sound level before they do your task.
Hope this helps,
Fabrice.
Hi @Fab,
Thanks, I will try out the Github method! I was also wondering about the Break if statement in the loop. Could I add something like Break if [response] = space so that they would then exit the loop upon pressing the space bar?
Hi @sflu,
You could try that method, but the "Break if" method runs full trials and then examines whether the loop should be interrupted. So if you have a long sound, pressing a key would not stop the sound right away. If you want to use a shorter sound that gets repeated many times, you could use that method and program some condition to break the loop. try it out and you'll quickly see whether that works for you.
Best,
Fabrice.