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: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:
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.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 theSND_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 theSND_ASYNC
flag.Putting that all together, we get a sound that loops continually and plays asynchronously:
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):
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!