[VB]程序中添加声音效果,使用资源文件,PlaySound And sndPlaySound使用方法
- Option Explicit
- '***********************
- 'ACC: How to Play .WAV Sounds on Events in Microsoft
- 'HOWTO: Play a Waveform (.WAV) Sound File in Visual Basic
- '经测试,播放资源中的WAV使用 sndPlaySound 实现的 PlayWaveRes ,
- ' 播放磁盘中文件使用PlaySound 实现的 PlaySystemWav
- '***********************
- Declare Function GetWindowsDirectoryA Lib "kernel32" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
- 'PlaySound 是 sndPlaySound 的增强版本
- Public Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
- Public Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (lpszSoundName As Any, ByVal uFlags As Long) As Long
- '*********************************************************************
- 'BOOL PlaySound(LPCSTR pszSound,HMODULE hmod,DWORD fdwSound);
- '
- '1.参数pszSound:是指定了要播放声音的字符串,该参数可以是WAVE文件的名字,或是WAVE资源的名字,
- '或是内存中声音数据的指针,或是在系统注册表WIN.INI中定义的系统事件声音.如果该参数为NULL则停止正在播放的声音.
- '
- '2.参数hmod:是应用程序的实例句柄,当播放WAV资源时要用到该参数,否则它必须为NULL.
- '
- '3.参数fdwSound: 是标志的组合,如下表所示。若成功则函数返回TRUE,否则返回FALSE。
- '*********************************************************************
- '*********************************************************************
- ' Flag values for wFlags parameter.
- '*********************************************************************
- Public Const SND_SYNC = &H0 ' Play synchronously (default)
- Public Const SND_ASYNC = &H1 ' Play asynchronously (see
- ' note below!)
- Public Const SND_NODEFAULT = &H2 ' Don't use default sound
- Public Const SND_MEMORY = &H4 ' lpszSoundName points to a
- ' memory file.
- Public Const SND_LOOP = &H8 ' Loop the sound until next
- ' sndPlaySound.
- Public Const SND_NOSTOP = &H10 ' Don't stop any currently
- ' playing sound.
- Public Enum SysWaveFile
- chimes = 1
- chord = 2
- ding = 3
- notify = 5
- recycle = 7
- ringin = 8
- ringout = 9
- Start = 10
- tada = 11
- End Enum
- '*********************************************************************
- ' Plays a wave file from a resource.
- '*********************************************************************
- Public Sub PlayWaveRes(vntResourceID As Variant, Optional vntFlags)
- '-----------------------------------------------------------------
- ' WARNING: If you want to play sound files asynchronously in
- ' Win32, then you MUST change bytSound() from a local
- ' variable to a module-level or static variable. Doing
- ' this prevents your array from being destroyed before
- ' sndPlaySound is complete. If you fail to do this, you
- ' will pass an invalid memory pointer, which will cause
- ' a GPF in the Multimedia Control Interface (MCI).
- '-----------------------------------------------------------------
- Static bytSound() As Byte ' Always store binary data in byte arrays!
- 'SND_ASYNC 异步播放注意事项,以保证引用在退出当前过程后仍有效.所以使用 Static
- bytSound = LoadResData(vntResourceID, "WAVE")
- If IsMissing(vntFlags) Then
- vntFlags = SND_NODEFAULT Or SND_ASYNC Or SND_MEMORY
- End If
- If (vntFlags And SND_MEMORY) = 0 Then
- vntFlags = vntFlags Or SND_MEMORY
- End If
- sndPlaySound bytSound(0), vntFlags
- End Sub
- '*********************************************************************
- ' Plays a wave file from a Windows/Media/
- '*********************************************************************
- '2007-04-10 05:05 55,776 chimes.wav 1
- '2007-04-10 05:05 97,016 chord.wav 2
- '2007-04-10 05:05 80,856 ding.wav 3
- '2007-04-10 05:05 24,253 flourish.mid 4
- '2007-04-10 05:05 119,384 notify.wav 5
- '2007-04-10 05:05 40,075 onestop.mid 6
- '2007-04-10 05:05 25,434 recycle.wav 7
- '2007-04-10 05:05 10,026 ringin.wav 8
- '2007-04-10 05:05 5,212 ringout.wav 9
- '2007-04-10 05:05 1,192 start.wav 10
- '2007-04-10 05:05 171,100 tada.wav 11
- '2007-04-10 05:05 22,097 town.mid 12
- Private Function GetWaveFileName(ByVal nFile As Integer)
- Select Case nFile
- Case 1
- GetWaveFileName = "/Media/Chimes.wav"
- Case 2
- GetWaveFileName = "/Media/chord.wav"
- Case 3, 4
- GetWaveFileName = "/Media/ding.wav"
- Case 5, 6
- GetWaveFileName = "/Media/notify.wav"
- Case 7
- GetWaveFileName = "/Media/recycle.wav"
- Case 8
- GetWaveFileName = "/Media/ringin.wav"
- Case 9
- GetWaveFileName = "/Media/ringout.wav"
- Case 10
- GetWaveFileName = "/Media/Start.wav"
- Case 11
- GetWaveFileName = "/Media/tada.wav"
- Case Else
- GetWaveFileName = "/Media/ding.wav"
- End Select
- End Function
- Public Sub PlaySystemWav(ByVal nFile As Integer)
- Dim sBuf As String
- Dim cSize As Long
- Dim retval As Long
- Static Windir As String
- 'SND_ASYNC 异步播放注意事项,以保证引用在退出当前过程后仍有效.所以使用 Static
- 'Create a variable large enough to store the Windows path.
- sBuf = String(255, 0)
- cSize = 255
- 'Get Windows Directory
- retval = GetWindowsDirectoryA(sBuf, cSize)
- 'Strip buffer from Windows directory
- Windir = Left(sBuf, retval)
- 'Load and Play the sound.
- Windir = Windir & GetWaveFileName(nFile)
- PlaySound Windir, vbNull, SND_ASYNC Or SND_NODEFAULT
- End Sub