VB API函数怎么播放音频文件
2个回答
展开全部
一,带窗口播放——
Private Declare Function shell Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Form_Load()
Call shell(Me.hwnd, "open", "1.wav", "", "D:/", 1)
End Sub
'==========================================================================================
' 参数 类型及说明
' hwnd Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框
' lpOperation String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它
' lpFile String,想用关联程序打印或打开一个程序名或文件名
' lpParameters String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数
' lpDirectory String,想使用的完整路径
' nShowCmd Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数
'==========================================================================================
二,不带窗口播放——
1,wav文件播放:
Private Declare Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Long
Private Sub Form_Load()
Call PlaySound("D:/1.wav", 0, 0)
End Sub
'lpszSoundName:歌名,hModule:应用程序等模块句柄,dwFlags:标志。
2,播放本地或网络mp3,wma文件
(1)无错误提示
Private Declare Function mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub Form_Load()
Call mciSendStringA("open D:/2.wma type MPEGVideo alias WMA", vbNullString, 0, 0)
Call mciSendStringA("play WMA wait", vbNullString, 0, 0)
Call mciSendStringA("close WMA", vbNullString, 0, 0)
End Sub
'lpstrCommand:MCI指令,lpstrReturnString:返回字串,uReturnLength:重播次数,hwndCallback:结束后活动窗口句柄
(2)有错误提示
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Private Sub Form_Load()
Call mciExecute("open D:/3.mp3 type MPEGVideo alias MP3")
Call mciExecute("play MP3 wait")
Call mciExecute("close MP3")
End Sub
'lpstrCommand:MCI指令
Private Declare Function shell Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Sub Form_Load()
Call shell(Me.hwnd, "open", "1.wav", "", "D:/", 1)
End Sub
'==========================================================================================
' 参数 类型及说明
' hwnd Long,指定一个窗口的句柄,有时候,windows程序有必要在创建自己的主窗口前显示一个消息框
' lpOperation String,指定字串“open”来打开lpFlie文档,或指定“Print”来打印它
' lpFile String,想用关联程序打印或打开一个程序名或文件名
' lpParameters String,如lpszFlie是可执行文件,则这个字串包含传递给执行程序的参数
' lpDirectory String,想使用的完整路径
' nShowCmd Long,定义了如何显示启动程序的常数值。参考ShowWindow函数的nCmdShow参数
'==========================================================================================
二,不带窗口播放——
1,wav文件播放:
Private Declare Function PlaySound Lib "winmm.dll" (ByVal lpszSoundName As String, ByVal hModule As Integer, ByVal dwFlags As Integer) As Long
Private Sub Form_Load()
Call PlaySound("D:/1.wav", 0, 0)
End Sub
'lpszSoundName:歌名,hModule:应用程序等模块句柄,dwFlags:标志。
2,播放本地或网络mp3,wma文件
(1)无错误提示
Private Declare Function mciSendStringA Lib "winmm.dll" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Sub Form_Load()
Call mciSendStringA("open D:/2.wma type MPEGVideo alias WMA", vbNullString, 0, 0)
Call mciSendStringA("play WMA wait", vbNullString, 0, 0)
Call mciSendStringA("close WMA", vbNullString, 0, 0)
End Sub
'lpstrCommand:MCI指令,lpstrReturnString:返回字串,uReturnLength:重播次数,hwndCallback:结束后活动窗口句柄
(2)有错误提示
Private Declare Function mciExecute Lib "winmm.dll" (ByVal lpstrCommand As String) As Long
Private Sub Form_Load()
Call mciExecute("open D:/3.mp3 type MPEGVideo alias MP3")
Call mciExecute("play MP3 wait")
Call mciExecute("close MP3")
End Sub
'lpstrCommand:MCI指令
展开全部
一般用mcisendstring函数,注意它只支持8.3格式路径,要额外用到getshortPathName()函数
下面是我自己编的示例函数
Private Function getVoicePath() As String '取得当前路径的8.3格式
Dim A As Integer, path As String, APPPATH As String
If Right(App.path, 1) = "\" Then path = App.path Else path = App.path & "\"
APPPATH = String$(165, 0)
A = GetShortPathName(path + "Voice\", APPPATH, 164)
getVoicePath = Left(APPPATH, InStr(APPPATH, Chr(0)) - 1)
End Function
Public Sub VoiceOn(name As String) '播放声音
VoiceName = getVoicePath & name & ".wav"
Res = mciSendString("play " & VoiceName, Ret, 1024, 0)
ID = mciGetDeviceID(VoiceName) '获得ID
End Sub
Public Sub VoiceOff() '关闭声音
Res = mciSendString("close all", Ret, 1024, 0)
End Sub
下面是我自己编的示例函数
Private Function getVoicePath() As String '取得当前路径的8.3格式
Dim A As Integer, path As String, APPPATH As String
If Right(App.path, 1) = "\" Then path = App.path Else path = App.path & "\"
APPPATH = String$(165, 0)
A = GetShortPathName(path + "Voice\", APPPATH, 164)
getVoicePath = Left(APPPATH, InStr(APPPATH, Chr(0)) - 1)
End Function
Public Sub VoiceOn(name As String) '播放声音
VoiceName = getVoicePath & name & ".wav"
Res = mciSendString("play " & VoiceName, Ret, 1024, 0)
ID = mciGetDeviceID(VoiceName) '获得ID
End Sub
Public Sub VoiceOff() '关闭声音
Res = mciSendString("close all", Ret, 1024, 0)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询