26(1).txt

来自「VB文章集(含API、窗口、数据库、多媒体、系统、文件、等等)」· 文本 代码 · 共 48 行

TXT
48
字号
不用ActiveX控件也能播放声音文件


Option Explicit 

Public Declare Function sndPlaySound Lib "winmm.dll" Alias _ 
"sndPlaySoundA" (ByVal lpszSoundName As String, _ 
ByVal uFlags As Long) As Long 

Const SND_SYNC = &H0 
Const SND_ASYNC = &H1 
Const SND_NODEFAULT = &H2 
Const SND_LOOP = &H8 
Const SND_NOSTOP = &H10 

接下来把下面的代码粘贴到你想播放声音文件的地方,比如放在命令按钮的Click事件中

Dim sFlags As Long 

sFlags = SND_ASYNC Or SND_NODEFAULT 

sndPlaySound "FileName.Wav", sFlags

把上面的FileName.Wav换成你想要播放的声音文件的完整路径及文件名就行了。

如果你仅仅是想播放一些Windows常用的声音,比如说退出Windows的声音,有一种更简单的写法

sndPlaySound "SystemExit",sFlags

将SystemExit换成下面这些参数,试试是什么声音?

SystemStart 
SystemExit 
SystemDefault 
SystemQuestion 
SystemAsterisk 
SystemExclamation 
SystemHand 

下面是sFlags所用参数的说明:

参数 说明 
SND_SYNC Plays the WAV file specified and returns only when the sound has stopped playing 
SND_ASYNC Plays the WAV file an continues after the sound has started playing. 
SND_NODEFAULT Do not play the default sound if the if the WAV file is not found. 
SND_LOOP Plays the WAV file continuously until sndPlaySound is called again. Remember to put SND_ASYNC in the sFlag variable as well. Else your application will lock-up. To stop playing the wave file set the "FileName.Wav" to Null instead. 
SND_NOSTOP Return to the beginning of the WAV file if it's already playing. 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?