VB : Music in form_load

Discussion in 'Software Q&A' started by deepak.krishnan, Aug 6, 2006.

Thread Status:
Not open for further replies.
  1. deepak.krishnan

    deepak.krishnan New Member

    Joined:
    Jan 11, 2005
    Messages:
    423
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Palakkad/Bangalore
    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.
     
  2. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    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
     
  3. OP
    OP
    deepak.krishnan

    deepak.krishnan New Member

    Joined:
    Jan 11, 2005
    Messages:
    423
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Palakkad/Bangalore
    Fine. I have a wav file in "e:\project\ " then what should I do? Also I want to loop the music.
     
  4. Venom

    Venom New Member

    Joined:
    Jun 13, 2006
    Messages:
    240
    Likes Received:
    2
    Trophy Points:
    0
    "c:\windows\media\tada.wav"
    Just replace that with your location.
     
  5. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    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: Aug 7, 2006
  6. OP
    OP
    deepak.krishnan

    deepak.krishnan New Member

    Joined:
    Jan 11, 2005
    Messages:
    423
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Palakkad/Bangalore
    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?
     
  7. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    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.
     
  8. OP
    OP
    deepak.krishnan

    deepak.krishnan New Member

    Joined:
    Jan 11, 2005
    Messages:
    423
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Palakkad/Bangalore
    So in the code the sound is looped?
     
  9. siriusb

    siriusb New Member

    Joined:
    May 12, 2005
    Messages:
    1,171
    Likes Received:
    1
    Trophy Points:
    0
    Location:
    Chennai, India, Asia, the Earth, the Solar system,
    ^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)
     
  10. deathvirus_me

    deathvirus_me New Member

    Joined:
    May 15, 2005
    Messages:
    1,838
    Likes Received:
    4
    Trophy Points:
    0
    Location:
    Kolkata, India
    Geez .. thanks a lot guys .... although never though of using this .. but now that i have a working code ... :D ...
     
  11. OP
    OP
    deepak.krishnan

    deepak.krishnan New Member

    Joined:
    Jan 11, 2005
    Messages:
    423
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Palakkad/Bangalore
    thanks yaar. It is working fine now. Thanks for your help.
     
Thread Status:
Not open for further replies.

Share This Page