你所需要的API函数称为PlaySound,以下为该函数的声明:
Public Declare Function PlaySound Lib "winmm.dll"
_
Alias "PlaySoundA" (ByVallpszName As String, _
ByValhModule As Long, ByValdwFlags As Long) _
As Long
在这一函数中,第一个变量是名称,包括运行的Wave文件的路径。第二个变量在运行文件时通常不使用,所以你可以将它赋值为zero。最后一个变量包含控制函数如何工作的标记。
为了当前的需要,必须需要两个标记。它们是:
所以,以下代码播放了DingDong.wav文件中的声音:
PlaySound "dingdong.wav", CLng(0), _
SND_ASYNC Or SND_FILENAME
当运行在一个程序中运行音频文件时,还有其他两点值得注意。第一,你必须确保一个特定的音频文件存在,否则会产生错误。这一过程可以用以下一个简单的函数来完成:
Public Function FileExists(FullFileName)
As Boolean
' Passed a filename (with path) returns
' True if the file exists, False if not.
Dim s
s = dir(FullFileName)
If s = "" Then
FileExists = False
Else
FileExists = True
End If
End Function