Results 1 to 4 of 4

Thread: Importing sound files

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    17

    Importing sound files

    I'm trying to make a program that will play sounds. Is it possible to import the sounds into the program so I don't have to direct the program towards a file? So I can just write something like

    VB Code:
    1. Play snd_car

    Instead of:

    VB Code:
    1. Play sound(C:\Audio\Sounds\car.wav)

    idk know if the "play" or "play sound" commands are real, I'm just using them for the sake of ease.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Importing sound files

    Coincidentally there is a PlaySound api function but you still must supply full path+file name:'
    VB Code:
    1. Private Const SND_APPLICATION = &H80  'look for application specific association
    2. Private Const SND_ALIAS = &H10000     'name is a WIN.INI [sounds] entry
    3. Private Const SND_ALIAS_ID = &H110000 'name is a WIN.INI [sounds] entry identifier
    4. Private Const SND_ASYNC = &H1         'play asynchronously
    5. Private Const SND_FILENAME = &H20000  'name is a file name
    6. Private Const SND_LOOP = &H8          'loop the sound until next sndPlaySound
    7. Private Const SND_MEMORY = &H4        'lpszSoundName points to a memory file
    8. Private Const SND_NODEFAULT = &H2     'silence not default, if sound not found
    9. Private Const SND_NOSTOP = &H10       'don't stop any currently playing sound
    10. Private Const SND_NOWAIT = &H2000     'don't wait if the driver is busy
    11. Private Const SND_PURGE = &H40        'purge non-static events for task
    12. Private Const SND_RESOURCE = &H40004  'name is a resource name or atom
    13. Private Const SND_SYNC = &H0          'play synchronously (default)
    14.  
    15. Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
    16.     (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    17.  
    18. Private Sub Form_Load()
    19.     PlaySound "C:\WINDOWS\MEDIA\TADA.WAV", ByVal 0&, SND_FILENAME Or SND_ASYNC
    20. End Sub

  3. #3
    Fanatic Member kregg's Avatar
    Join Date
    Feb 2006
    Location
    UK
    Posts
    524

    Re: Importing sound files

    You can try using the resource option in visual basic.

    Go to Add-Ins>Add-In Manager.

    Select "VB 6 Resource Editor"
    Tick "Loaded/Unloaded"
    Press OK

    Press Tools>Resource Editor
    And from the second button to the right at the top of the new window you see, then add your file.

    After that, I think you have to use the LoadRes() or something. This part of the Resource Editor I'm not too sure of.

  4. #4
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Importing sound files

    There's a link to a very good tutorial on resource files in my signature. To play the sounds from the res file:
    VB Code:
    1. Option Explicit
    2.  
    3. 'Form level code.
    4.  
    5. 'YOU MUST COMPILE TO AN EXECUTABLE TO MAKE IT WORK !!!
    6. '-----------------------------------------------------
    7.  
    8. Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
    9.    (ByVal lpszName As Long, ByVal hModule As Long, ByVal dwFlags As Long) As Long
    10.  
    11. 'Required constants.
    12. Const SND_ASYNC = &H1 'Play asynchronously.
    13. Const SND_RESOURCE = &H40004 'Name is a resource name or atom.
    14. Const SND_PURGE = &H40 'Release memory.
    15.  
    16. 'Toggle playing by clicking on the form.
    17. Dim OnOff As Boolean
    18.  
    19. Private Sub Form_Click()
    20. 'Alternate clicks on the form will start/stop playback.
    21.     Dim RetVal As Long
    22.    
    23.     If OnOff = True Then
    24.         OnOff = False
    25. 'Reference nothing and purge memory.
    26.         RetVal = PlaySound(0, App.hInstance, SND_PURGE)
    27.     ElseIf OnOff = False Then
    28.         OnOff = True
    29. 'Reference the res file (101)
    30.         RetVal = PlaySound(101, App.hInstance, SND_RESOURCE Or SND_ASYNC)
    31.     End If
    32.     Caption = OnOff
    33. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width