I would like to loop through audio in VB.NET.
Here is my code:
While blnAlert = True
My.Computer.Audio.Play("C:\cat_1.wav")
End While
But it freezes the app.
Cheers.
I would like to loop through audio in VB.NET.
Here is my code:
While blnAlert = True
My.Computer.Audio.Play("C:\cat_1.wav")
End While
But it freezes the app.
Cheers.
The code as you've written it will loop without end. The loop conditional is the following
While blnAlert = True
If this is true the loop will be entered. The code inside the loop does nothing to alter this value and hence the condition will always be true. This means the loop will not end.
How do you expect the value of blnAlert to be updated? Can you post some more code or give us a bit more context into the problem?
EDIT OP indicated that the blnAlert will be set to False via button press
The problem is that as the code is written the cancel button cannot be pressed. As soon as the while loop is hit it will not exit. The app will freeze because while your code is executing the form is not able to repaint or handle any user input. You must return control to the application in order to press the cancel button.
Likely the easiest way to solve this problem is to use a timer. Create a timer and during the timer tick function play the sound a single time and leave. Use the cancel button to stop the timer and hence the playing of the sound.
I know the answer given by JaredPar will work and has been accepted, but here's a different (and simpler) solution using the fact that My.Computer.Audio.Play also accepts optional parameters including AudioPlayMode.BackgroundLoop which plays a sound without halting execution until My.Computer.Audio.Stop is called.
So, start playing the sound with:
My.Computer.Audio.Play("mysound.wav", AudioPlayMode.BackgroundLoop)
and in the button-click event:
My.Computer.Audio.Stop()