VB API函数怎么播放音频文件

 我来答
API_Studier
2009-06-07 · TA获得超过295个赞
知道小有建树答主
回答量:97
采纳率:100%
帮助的人:121万
展开全部
一,带窗口播放——

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指令
百度网友19de9c005
2009-05-29 · 超过32用户采纳过TA的回答
知道答主
回答量:81
采纳率:25%
帮助的人:65.2万
展开全部
一般用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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式