Stop sound from replaying in while loop

I am trying to make a sound play continuously if a certain time has passed. Below is my code. However, the sound keeps replaying instead of one continuous sound as it is in a while loop. Putting it outside the while loop will not work as the time is incremented inside the while loop. How do I go about doing this? Please help! I have been stuck at this for 2 days..... This is an issue with my ordering of my coding, so its nothing to do with the sound being played correctly or not.
Thanks!
t = 0;
pahandle = PsychPortAudio('Open', [], 1, 1, 48000, 2);
myBeep = MakeBeep(500, 0.5, 48000);
PsychPortAudio('RunMode', pahandle, 1);
PsychPortAudio('FillBuffer', pahandle, [myBeep; myBeep]);
while t < 20
t = t + 1;
if ~KbCheck && t >= 5
PsychPortAudio('Start', pahandle, 0, 0, 1);
elseif KbCheck
PsychPortAudio('Stop', pahandle, 1, 1);
break
elseif t > 20
PsychPortAudio('Stop', pahandle, 1, 1);
break
end
end
PsychPortAudio('Close', pahandle);

回答(1 个)

At t == 6 your condition
if ~KbCheck && t >= 5
is going to be true, so you are going to re-issue the start.

类别

帮助中心File Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by