How can I use PlaySound() to play a .wav file? I have PlaySound(sound) but I keep getting error "Argument not optional".
Also how do I stop playing a sound?
How can I use PlaySound() to play a .wav file? I have PlaySound(sound) but I keep getting error "Argument not optional".
Also how do I stop playing a sound?
You don't show us any code in your question, so I'll just have to guess at what might be wrong.
First, the PlaySound
function needs to be correctly declared in your VB 6 code:
Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
And you need some constants, which you can find easily using the API Viewer application. Here's a list I pulled off the web since I don't have VB 6 installed:
Private Const SND_APPLICATION As Long = &H80
Private Const SND_ALIAS As Long = &H10000
Private Const SND_ALIAS_ID As Long = &H110000
Private Const SND_ASYNC As Long = &H1
Private Const SND_FILENAME As Long = &H20000
Private Const SND_LOOP As Long = &H8
Private Const SND_MEMORY As Long = &H4
Private Const SND_NODEFAULT As Long = &H2
Private Const SND_NOSTOP As Long = &H10
Private Const SND_NOWAIT As Long = &H2000
Private Const SND_PURGE As Long = &H40
Private Const SND_RESOURCE As Long = &H40004
Private Const SND_SYNC As Long = &H0
You will need to consult the SDK documentation to learn what each of those magic numbers mean or do. While you're there, you should also read up on the meaning of the function's parameters. From the error you're getting, it sounds like you're attempting to call the function incorrectly. In particular, you're omitting one of the three arguments and they're all required (i.e., not marked Optional
).
I don't know what kind of sound you want to play, or where the sound file is located, so I can't give an example that perfectly captures your situation. But, to play a sound from a file on disk, you would pass the full path to the sound file as the first argument, 0 for the second argument (because you're not loading a sound from a resource), and SND_FILENAME
for the third argument.
PlaySound "X:\Sounds\trumpet.wav", 0, SND_FILENAME
You can also add the SND_ASYNC
flag to play the sound asynchronously. This means that the function will return immediately and allow the sound the play in the background while the rest of your code executes. This is instead of the default behavior, achieved explicitly with the SND_SYNC
flag.
And you can add the SND_LOOP
flag to cause the sound to play repeatedly until you stop it. Naturally, this also requires the SND_ASYNC
flag.
Putting that all together, we get a sound that loops continually and plays asynchronously:
PlaySound "X:\Sounds\trumpet.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_LOOP
To stop the sound from playing, you pass a null string for the first argument (because you don't need to specify a sound to play), 0 for the second argument again, and 0 for the third argument (because you don't need any special behavior):
PlaySound vbNullString, 0, 0
Naturally, this will only work if you passed the SND_ASYNC
flag when you initially began playing the sound. Otherwise, control won't return to your code until the sound has already finished playing, and then there will be nothing to stop!