• Best of luck to the class of 2024 for their HSC exams. You got this!
    Let us know your thoughts on the HSC exams here
  • YOU can help the next generation of students in the community!
    Share your trial papers and notes on our Notes & Resources page
MedVision ad

Sounds in Visual Basic 6.0 (1 Viewer)

lawrencema_1985

New Member
Joined
Aug 9, 2003
Messages
23
Gender
Male
HSC
2003
Just a quick question...how do you play sounds (*.wav) in response to events such as a mouse button click in visual basic 6.0?

I'm making a clay pigeon shooter game and i just want to add sound to the shots being fired and the pigeons being hit.

Cheers.
 
Last edited:

:: ck ::

Actuarial Boy
Joined
Jan 1, 2003
Messages
2,414
Gender
Male
HSC
2004
i think theres two ways.. api call function n some other way... can't remember sorry
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
it's better to use the winnm.dll call


search it up on google
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
Yeah - go with winmm.dll (typo above winston)

Code:
' Put these in the form (general) (declarations)
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Declare Function waveOutGetNumDevs Lib "winmm" () As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
'
' Put this in a command button click event or whatever event you need the sound to play under
Private Sub Command1_Click()
Dim ret, flags As Integer
Dim soundfilename As String
soundfilename = "c:\whatever.wav"
flags = SND_ASYNC Or SND_NODEFAULT
ret = waveOutGetNumDevs()
If ret > 0 Then
  ret = sndPlaySound(soundfilename, flags)
Else
  MsgBox "NO SOUND DEVICE"
End If
End Sub
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Originally posted by Fosweb
Yeah - go with winmm.dll (typo above winston)

Code:
' Put these in the form (general) (declarations)
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Declare Function waveOutGetNumDevs Lib "winmm" () As Long
Const SND_SYNC = &H0
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_LOOP = &H8
Const SND_NOSTOP = &H10
'
' Put this in a command button click event or whatever event you need the sound to play under
Private Sub Command1_Click()
Dim ret, flags As Integer
Dim soundfilename As String
soundfilename = "c:\whatever.wav"
flags = SND_ASYNC Or SND_NODEFAULT
ret = waveOutGetNumDevs()
If ret > 0 Then
  ret = sndPlaySound(soundfilename, flags)
Else
  MsgBox "NO SOUND DEVICE"
End If
End Sub

oops must of been high yesterday lol...

and if you wanna stop the sound, you just need to provide a Null source, and play it
 

lawrencema_1985

New Member
Joined
Aug 9, 2003
Messages
23
Gender
Male
HSC
2003
thanks guys, esp fosweb...that worked.

What about playing two sounds at the same time?
 

Fosweb

I could be your Doctor...
Joined
Jun 20, 2003
Messages
594
Location
UNSW. Still.
Gender
Male
HSC
2003
You wont be able to do that with winmm.dll i think.

If its really important, then maybe look up using DirectSound (i've never done this - i think there was a thread on it before though - in which i said exactly the same thing...)

Edit: It was in a thread called "Format C: in VB", but dont bother with that.

There are some fairly simple controls floating around that will do the job for you. I also havent tried this one, but check it out: http://www.a1vbcode.com/app.asp?ID=1680
Just download...
 
Last edited:

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Originally posted by Fosweb
You wont be able to do that with winmm.dll i think.

If its really important, then maybe look up using DirectSound (i've never done this - i think there was a thread on it before though - in which i said exactly the same thing...)

Edit: It was in a thread called "Format C: in VB", but dont bother with that.

There are some fairly simple controls floating around that will do the job for you. I also havent tried this one, but check it out: http://www.a1vbcode.com/app.asp?ID=1680
Just download...
There may be a work around in VB 6, which will require alot of researching, but in .NET it's fairly powerful to the level you can actually play two sounds at once using that API call, providing you know how to use your delegates and threading.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top