Stop sound at the end of trial

Hi,

[EDIT: I was able to circumvent the issue but I do not consider it a solution, so I am still looking for ideas/suggestions]

I do not seem to be able to stop a sound from playing at the end of the trial and it leaks to the next trial/routine.
I define my sound component using code.

I know the sound_2.status === PsychoJS.Status.STARTED at the End Routine evaluates as I get PASSED! printed in the console. Therefore, I do not know why the sound_2.stop(); does not work.

Each Frame:

if (mouse.isPressedIn(bubble) && (sound_2.status === PsychoJS.Status.NOT_STARTED)){ 
      sound_2.tStart = t; 
      sound_2.frameNStart = frameN; 
      psychoJS.window.callOnFlip(function(){ sound_2.play(); });  
      sound_2.status = PsychoJS.Status.STARTED;
    }
    
if (t >= (sound_2.getDuration() + sound_2.tStart) && sound_2.status === PsychoJS.Status.STARTED) {
      sound_2.stop();  
      sound_2.status = PsychoJS.Status.FINISHED;
    }
//allow for sound to be played every time the speech bubble is clicked on
if (mouse.isPressedIn(bubble) && (sound_2.status === PsychoJS.Status.FINISHED)){ 
    sound_2.play();
    sound_2.tStart = t; 
    sound_2.frameNStart = frameN; 
    psychoJS.window.callOnFlip(function(){ sound_2.play(); });
    sound_2.status = PsychoJS.Status.STARTED;
    };

End Routine

if (sound_2.status === PsychoJS.Status.STARTED) { //if sounds is still being played
    console.log("PASSED!");
    sound_2.stop();  
    sound_2.status = PsychoJS.Status.FINISHED;
    }

Any help will be much appreciated.

Thanks
Yiannis

I had similar problem yesterday where a sound was supposed to stop on a count of thisN == 23, but it stopped before the end of the trial. I moved the code from the end routine tab into a new code element at the end of the routine. Hope that makes sense. I am sure there are more elegant ways but it works now and stops at the end of the final stimulus.

1 Like

Hi @Glen ,

Many thanks for the suggestion.
I do have another exp which is similar to yours i.e. start sound from the beginning of the exp and end it after X number of trials or when a certain conditions is evaluated. The sound.stop() in that exp works as intended. But for this particular exp I posted, it doesn’t. I am sure I am doing something wrong but can’t figure out what.

Best wishes
Yiannis