How to play a wav file (in four ways)
Posted on August 9, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | 'MultiMedia - How to play a wav file (in four ways) Option Explicit 'Method 1: 'This code uses the Playsound API function. Playsound can read files from standard WAV files on 'disk, by reference from the system registry, or from a resource file. For the application 'developer, this means that you can create a registry entry for your sound effects, which means 'that the end user can use the control panel to customize their preferences in sounds. You avoid 'designing another configuration screen or panel, and the user can still customize the application. 'lpszName points to a registry entry 'Do not use SND_RESOURSE or SND_FILENAME Private Const SND_ALIAS& = &H10000 'Playsound returns immediately 'Do not use SND_SYNC Private Const SND_ASYNC& = &H1 'The name of a wave file. 'Do not use with SND_RESOURCE or SND_ALIAS Private Const SND_FILENAME& = &H20000 'Unless used, the default beep will 'play if the specified resource is missing Private Const SND_NODEFAULT& = &H2 'Fail the call & do not wait for 'a sound device if it is otherwise unavailable Private Const SND_NOWAIT& = &H2000 'Use a resource file as the source. 'Do not use with SND_ALIAS or SND_FILENAME Private Const SND_RESOURCE& = &H40004 'Playsound will not return until the 'specified sound has played. Do not 'use with SND_ASYNC Private Const SND_SYNC& = &H0 Public Enum enSound_Source ssFile = SND_FILENAME& ssRegistry = SND_ALIAS& End Enum 'These are common sounds available from the registry Public Const elDefault = ".Default" Public Const elGPF = "AppGPFault" Public Const elClose = "Close" Public Const elEmptyRecycleBin = "EmptyRecycleBin" Public Const elMailBeep = "MailBeep" Public Const elMaximize = "Maximize" Public Const elMenuCommand = "MenuCommand" Public Const elMenuPopUp = "MenuPopup" Public Const elMinimize = "Minimize" Public Const elOpen = "Open" Public Const elRestoreDown = "RestoreDown" Public Const elRestoreUp = "RestoreUp" Public Const elSystemAsterisk = "SystemAsterisk" Public Const elSystemExclaimation = "SystemExclaimation" Public Const elSystemExit = "SystemExit" Public Const elSystemHand = "SystemHand" Public Const elSystemQuestion = "SystemQuestion" Public Const elSystemStart = "SystemStart" Private Declare Function PlaySound& Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _ ByVal hModule As Long, ByVal dwFlags As Long) 'hModule is only used if SND_RESOURCE& is set and represents 'an HINSTANCE handle. This example doesn't support playing 'from a resource file. 'Plays sounds from the registry or a disk file 'Doesn't care if the file is missing Public Function EZPlay(ssname As String, sound_source As enSound_Source) As Boolean If PlaySound(ssname, 0&, sound_source + SND_ASYNC + SND_NODEFAULT) Then EZPlay = True Else EZPlay = False End If End Function 'How to call this function 'Call EZPlay("C:\WINDOWS\media\chimes.wav",ssFile) 'Method 2: Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, _ ByVal uFlags As Long) As Long Sub Main() sndPlaySound "C:\WINDOWS\Media\chimes.wav", 1 'the '1' following the file means that the program should not stop to play the file. 'The sound will play and other events can be happening. 'If you want the whole program to stop while the sound is playing, just change the '1' to '0'. End Sub 'Method 3: Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, _ ByVal hModule As Long, ByVal dwFlags As Long) As Long Private Sub Form_Load() Dim lRet As Long lRet = PlaySound("C:\MyWavFile.wav", 0&, 0&) End Sub 'Method 4: Option Explicit Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _ (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _ (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long Private Const SND_ASYNC = &H1 Private Const SND_FILENAME = &H20000 Private Const SND_SYNC = &H0 Private Function SndPlay(ByVal filename As String, Optional ByVal options As Long = _ (SND_FILENAME Or SND_ASYNC)) As Long SndPlay = sndPlaySound(filename, options) End Function Private Function SndPlayEx(ByVal filename As String, Optional ByVal lmodule As Long = 0, _ Optional ByVal options As Long = (SND_FILENAME Or SND_ASYNC)) As Long SndPlayEx = PlaySound(filename, lmodule, options) End Function ' Open a new project and add two command buttons(Command1,Command2)in the form ' NOTE: change the filename to a valid WAV file.. Private Sub Command1_Click() SndPlay "c:\windows\media\tada.wav" End Sub Private Sub Command2_Click() SndPlayEx "c:\windows\media\tada.wav" End Sub |