title: Cannot stop sound from replaying in while loop
body: <p>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.</p>
  
  <p>Thanks!</p>
  
  <pre><code>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 &lt; 20
      t = t + 1;
  
      if ~KbCheck &amp;&amp; t &gt;= 5 
          PsychPortAudio('Start', pahandle, 0, 0, 1);
  
      elseif KbCheck
          PsychPortAudio('Stop', pahandle, 1, 1);
          break
      elseif  t &gt; 20
          PsychPortAudio('Stop', pahandle, 1, 1);
          break
      end
  end
  
  PsychPortAudio('Close', pahandle);
  </code></pre>