以下代码放入标准模块
Public Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
例如播放一个MP3文件
Dim RefShortName As String
Dim plays As Long
GetShortPathName App.Path & "\OnTime.Mp3", ShortPathName, 255
RefShortName = Left(ShortPathName, InStr(1, ShortPathName, Chr(0)) - 1)
plays = mciSendString("play " & RefShortName, vbNull, 0, 0) '播放
plays = mciSendString("close " & RefShortName, vbNull, 0, 0) '停止播放
Mid文件类似
怎样使用API播放MIDI音乐?
本例将演示如何使用mciSendString播放MIDI文件,如果你有专业版或企业版的VB环境,可以直接使用MCI控件播放MIDI,而无需使用API。
请按照以下步骤进行:
首先建立一个新的项目文件,添加一个命令按钮,并向按钮添加以下代码:
Private Sub Command1_Click()
Dim ret As Integer
' 下面的代码将打开C:\WIN31\CANYON.MID的音序器
ret = mciSendString("open c:\windows\CANYON.MID _
type sequencer alias canyon", 0&, 0, 0)
'直到MCI命令返回到程序
ret = mciSendString("play canyon wait", 0&, 0, 0) ' 关闭Canyon
ret = mciSendString("close canyon", 0&, 0, 0)
End Sub
在Form1的general declarations中加入以下代码:
#If Win32 Then
Private Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
#ElseIf Win16 Then
Private Declare Function mciSendString Lib "mmsystem" (ByVal _
lpstrCommand As String, ByVal lpstrReturnStr As Any, ByVal _
wReturnLen As Integer, ByVal hCallBack As Integer) As Long