If I have an alarm clock and when the alarm goes off at a specified time, I use the Beep function. How do I change the code to play a wave file instead of Beep function? Is there a simple way?
Printable View
If I have an alarm clock and when the alarm goes off at a specified time, I use the Beep function. How do I change the code to play a wave file instead of Beep function? Is there a simple way?
Use the PlaySound API.
Code:Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Sub Command1_Click()
'Play the WAV
PlaySound "C:\MyFile.wav", 0&, &H1
End Sub
If Megatron's code doesn't work, you could also use:
Code:Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Global Const SND_ASYNC = &H1
Global Const SND_NODEFAULT = &H2
Sub Playwav(file)
SoundName$ = file
wFlags% = SND_ASYNC Or SND_NODEFAULT
X% = sndPlaySound(SoundName$, wFlags%): NoFreeze% = DoEvents()
End Sub
Usage:
'Play
'Playwav "C:\wavfile.wav"
'Stop
'Playwav " "
Keep in mind that sndPlaySound is superseded by PlaySound, hence it's obsolete.