Visual Basic Wiki
Advertisement
'Note: this will only work with small waveform audio files.
'If you need more advanced functionality, consider using OpenAL + Vorbis.
'OpenAL is much more versatile, e.g. allowing you to use e.g. 3D positional audio.
'Vorbis can be used to compress your sound effects, making them much smaller then waveform audio would be.
'See e.g.: http://www.gamedev.net/reference/articles/article2031.asp

'Note: you can combine these flags using Or. Don't use + to combine flags.
Private Const sndAsync = &H1        'Play asynchronously (default: synchronously)
Private Const sndNoDefault = &H2    'If no sound can be found, be silent (default: system beep)
Private Const sndMemory = &H4       'Name points to a memory file
Private Const sndLoop = &H9         'Loop the sound until next PlaySound
Private Const sndNoStop = &H10      'Don't stop any currently playing sound
Private Const sndApplication = &H80 'Look for application specific association
Private Const sndPurge = &H40       'Purge non-static events for task
Private Const sndNoWait = &H2000    'Don't wait if the driver is busy
Private Const sndAlias = &H10000    'Name is a WIN.INI [sounds] entry
Private Const sndAliasID = &H110000 'Name is a WIN.INI [sounds] entry identifier
Private Const sndFilename = &H20000 'Name is a file name; it will not be interpreted as anything else.
Private Const sndResource = &H40004 'Name is a resource name or atom
Private Declare Function PlaySoundA Lib "WinMM" (ByVal Name As String, ByVal hModule As Long, ByVal Flags As Long) As Long
Private Declare Function PlaySoundW Lib "WinMM" (Name As Byte, ByVal hModule As Long, ByVal Flags As Long) As Long

Private Sub Form_Click()
Dim B() As Byte
Select Case 0 'Change this value to try the other examples.
Case 0
    'PlaySoundA is easiest to use, but doesn't work if the filename contains characters not in the current codepage.
    PlaySoundA "C:\WINDOWS\Media\chimes.wav", 0, sndAsync
Case 1
    'PlaySoundW uses Unicode, so it's more robust than PlaySoundA. However, it doesn't work on Windows 9x without an emulation layer.
    B = "C:\WINDOWS\Media\chimes.wav"
    PlaySoundW B(0), 0, sndAsync
Case 2
    'Suppose you want to read your sound effects from a file. Try this:
    Open "C:\WINDOWS\Media\chimes.wav" For Binary Access Read As #1
    ReDim B(1 To LOF(1))
    Get #1, , B
    Close #1
    PlaySoundW B(1), 0, sndAsync Or sndMemory
Case 3
    'You can also play system sounds.
    PlaySoundA "SystemAsterisk", 0, sndAsync 'See HKEY_CURRENT_USER\AppEvents\EventLabels
End Select
End Sub
Advertisement