VB : Music in form_load

Status
Not open for further replies.

deepak.krishnan

In the zone
Hello friends,
I would like to play a .wav file on form load in my project. Can you please help me with its coding? It is urgent.
 

siriusb

Cyborg Agent
Next time, google it out/

Code:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found

Private Sub Form_Click()
    Dim ret As Boolean
    ret = sndPlaySound("c:\windows\media\tada.wav", (SND_ASYNC Or SND_FILENAME Or SND_NODEFAULT))
    If ret = False Then
        MsgBox "Error"
    End If
End Sub
 

siriusb

Cyborg Agent
Code:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Const SND_ASYNC = &H1         '  play asynchronously
Private Const SND_FILENAME = &H20000     '  name is a file name
Private Const SND_NODEFAULT = &H2         '  silence not default, if sound not found
Private Const SND_LOOP = &H8         '  loop the sound until next sndPlaySound

Private Sub Form_Load()
    Dim ret As Boolean
    ret = sndPlaySound("e:\project\tada.wav", (SND_ASYNC Or SND_FILENAME Or SND_NODEFAULT Or SND_LOOP))
    If ret = False Then
        MsgBox "Error"
    End If
End Sub

Check your API Viewer tool for more constants for the api.
 
Last edited:
OP
deepak.krishnan

deepak.krishnan

In the zone
But then what about this:
sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Can you please tell me what it indicates?
 

siriusb

Cyborg Agent
That line means that we are declaring to vb that we want to call the api named snPlaySound defined in winmm.dll (comes with windows) in our program. For further reference, go to msdn.microsoft.com and check out api programming.
 

siriusb

Cyborg Agent
^Yes. Seeing as you've asked me that (and haven't gone mad), I'll assume you haven't run the code yet. Anyways, here's the code to stop the playing looped sound.
Code:
call sndPlaySound (vbNullString, SND_NODEFAULT)
 
Status
Not open for further replies.
Top Bottom