Tech Support Guy banner

MS Access - Play wav on a loop

1800 Views 7 Replies 3 Participants Last post by  Gram123
Hiyo.
I'm looking for a bit of code to apply to an On/Off toggle button.
What I need it to do is, when the button is clicked to On, play a wav sound intermittently (once every 60 seconds), until the user switches the button to Off.

I'm guessing the basics will be something along the lines of:

IF togglebtn = -1 THEN
DO UNTIL togglebtn = 0​
sndPlaySound(“wav file location/filename.wav”)​
SLEEP 60000​
LOOP​
ELSE
END IF

There may be better ways to do this (e.g. is Sleep the best timer option?), and I've seen people discussing embedding a wav file into the db rather than playing an external file?

Any help appreciated!
Status
Not open for further replies.
1 - 8 of 8 Posts
That looks good to me, not that I have ever played a sound file, the only thing I would say is that the "Else" is redundant.
If it works use it, I am not sure about embedding the file in the database, you could try it and see how much space it takes up and if it makes it any faster to respond.
You need to add a function to play the sound:

Code:
Public Declare Function sndPlaySound Lib "winmm.dll" _
Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Then your button_click (you might need to fig about with this as I haven't checked the togglebutton values. But you should note the proper syntax for calling the wav file:

Code:
Sub Button1_Click()


If togglebtn = -1 THEN
[INDENT]Do until togglebtn = 0[/INDENT][INDENT][INDENT]sndPlaySound “wav file location/filename.wav”, 0[/INDENT][/INDENT][INDENT][INDENT]SLEEP 60000[/INDENT][/INDENT][INDENT]Loop[/INDENT]End If
End Sub
See less See more
Ok, as a test, I created a quick form (Form1), with a button (Toggle1) with its Default Value set to 0.

I added the Public Declare Function, as noted by CodeLexicon.
Then on the OnClick event of the button, I entered:

If Toggle1 = -1 Then
Do Until Toggle1 = 0
sndPlaySound "C:\Users\<me>\Desktop\horn.wav", 0
Sleep 5000
Loop

End If
End Sub
It didn't recognise "Sleep", so I found advice saying the following was needed in the module:

Private Declare Sub Sleep Lib "kernel32" (ByVal lngMilliSeconds As Long)
On clicking the button, it successfully played the wav every 5 seconds. However, when trying to click the Toggle1 button again to turn the alarm off, Access hangs (Not Responding). Actually, if you leave it going for 10 secs or so without clicking anything, the Access title bar says Not Responding anyway, and clicking in the Access window therafter results in a faded not responding window, with the wav still playing.

I can CTRL + Break to interrupt the code, click End, then I'm able to click the button to "off". Then I can click it to set it going again.

So, is there a way to break the loop / avoid Access going into "not responding", without having to CTRL + Break to interrupt the code?

Cheers!
See less See more
I think esc or Ctrl+Break are your only option. My gut feel is that if this is causing Access to hang, you maybe need to look at alternative approaches ... after all you wouldn't want your innocent wav to be the cause of user frustration and lost work. One thought I had was that you dispense with the sleep option and create a wav with required blank space at the end - I wonder if CP's code (see the loop option: Const SND_LOOP = &H8) might be worth playing with.

http://www.cpearson.com/excel/PlaySound.aspx
You could try putting in a For/Next loop that just Counts instead of the sleep function, you could also remove the Do until loop and use the very old fashioned "Goto" function.
But that still leaves the code running when you are trying to click the stop button.
Have you looked at the Form's "On Timer" and Timer Interval functions to do this?
OK - here's some more code grabbed from the net:

This one demonstrates that you can get the sound looping

Code:
Option Explicit
Private Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long


Sub PlayWav()
Dim WavFile As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_FILENAME = &H20000
On Error Resume Next
WavFile = "C:\Users\<me>\Desktop\horn.wav"
Call PlaySound(WavFile, 0&, SND_ASYNC Or SND_LOOP)
End Sub
Ok, so that works looping. So I was thinking on click you start a sub to check the toggle value. If the value is "on" it goes to the sub to play the loop. if it is "off" it goes to the sub to stop the sound. At the moment I haven't worked out the code to stop, other than to play the sound again without the loop specified:

Code:
Sub STOPSOUND()
Dim WavFile As String
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_LOOP = &H8
Const SND_FILENAME = &H20000
On Error Resume Next
WavFile = "C:\Users\<me>\Desktop\horn.wav"
Call PlaySound(WavFile, 0&, 0)
End Sub
I've tested this roughly and Excel doesn't hang at least. But you would still need a file with the 60 sec pause in.
See less See more
Will look into this one further in the near future. Thanks for your input chaps!
1 - 8 of 8 Posts
Status
Not open for further replies.
Top