Is there an easy way to play a sound using an if statement?
Printable View
Is there an easy way to play a sound using an if statement?
What kind of sound? An If statement "conditionally executes a group of statements, depending on the value of an expression".
Did you mean by "easy" that you want to do as little programming as possible?
You could write a reusable function call inside of any if statements for playing sound.
Although I do not know how to play sound in VB6, I do know from working in other languages that most sounds that you want to play have to be streamed from a file.
That is... you need to obtain a buffer (or array) that can hold X amount of bytes of sound data. You then read data from the sound file into this buffer, and play it.
You would loop through the entire contents of the file until all the sound data is played.
I warn you though, if you load the whole file in one big buffer, then you will consume much more RAM than you need to.
I hope this helps in your search for whatever it is you are trying to find out. (If you spent some more time explaining thoroughly what issue you were having, it would benefit you by letting everyone know how we can help).
Hi,
Thanks, I wasn't very clear!
The function idea is exactly what i'm looking for.
I want to be able to call the sound in whenever I want it.
its a small file - a ringing bell to signal the end of a round.
How would I go about putting this into a function?
Thanks again,
Klivingston
Your level of naiveté raises some red flags.
VB6 is far from easy to come by today. Are you sure you are not using VB.Net instead? If so, you are in the wrong forum.
Note that we don't help people using stolen copies of VB6 (e.g. "Portable Edition").
No, I am most certainly using VB6. Schools still teach using visual basic 6.
If you put the following code in a new Module in your project, you can call it each time you want to produce a 'sound':
You 'call' it by something like this:Code:Public Declare Function PlaySound _
Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long
Public Const SND_ASYNC = &H1
Public Const SND_LOOP = &H8
Public Const SND_MEMORY = &H4
Public Const SND_NODEFAULT = &H2
Public Const SND_NOSTOP = &H10
Public Const SND_SYNC = &H0
Public Const SND_ALIAS = &H10000
Public Const SND_APPLICATION = &H80
Public Const SND_ALIAS_ID = &H110000
Public Const SND_FILENAME = &H20000
Public Const SND_NOWAIT = &H2000
Public Const SND_PURGE = &H40
Public Const SND_RESOURCE = &H40004
Public gbResults As Boolean
orCode:gbResults = playsound(mySound, 0, SND_ASYNC)
Where mySound is a string variable holding the path&fileName of the sound file.Code:gbResults = playsound(mySound, 0, SND_SYNC)
You can also use the MMC (Multimedia control) if you prefer. Also, the code I sent plays wave (.wav) files. And the MMC can play .mp3 files. Not sure what other formats each play.....I use either, depending upon if I have .wav or .mp3 files, although, like I say, each may play different formats as well.
Play and Stop Sound Example