|
|

楼主 |
发表于 2022-1-21 11:27
|
显示全部楼层
分享:API播放本地wav音频功能今后音频相关的API分享都放在这一个帖子里,欢迎大家加入讨论!
本功能可以用于VBA程序开始及结束时播放系统提示音,不仅限于Msgbox文字提醒用户。
主要方法:兼容64位及32位的API函数sndPlaySound,程序PlayTheSound
- Option Explicit
- #If VBA7 Then
- Public Declare PtrSafe Function sndPlaySound Lib "winmm.dll" _
- Alias "sndPlaySoundA" ( _
- ByVal lpszSoundName As String, _
- ByVal uFlags As Long) As Long
- #Else
- Public Declare Function sndPlaySound Lib "winmm.dll" _
- Alias "sndPlaySoundA" ( _
- ByVal lpszSoundName As String, _
- ByVal uFlags As Long) As Long
- #End If
- Public Sub PlayTheSound(ByVal WhatSound As String, Optional Flags As Long = 0)
- If InStr(1, WhatSound, ".") = 0 Then
- WhatSound = WhatSound & ".wav"
- End If
- If Dir(WhatSound) = vbNullString Then
- MsgBox "Wav file not found."
- Exit Sub
- End If
- sndPlaySound WhatSound, Flags
- End Sub
复制代码 下面的代码可以列出系统自带的wav文件名称及路径:
- Sub ListSystemWavFiles()
- Dim FSO As Object, fsoFolder As Object, fsoFile As Object
- Dim i As Long
-
- Set FSO = CreateObject("Scripting.FileSystemObject")
- Set fsoFolder = FSO.GetFolder(Environ("SystemRoot") & "\Media")
- i = 1
- For Each fsoFile In fsoFolder.Files
- If InStr(fsoFile.Name, "wav") Then
- i = i + 1
- Cells(i, 1) = fsoFile.Name
- Cells(i, 2) = fsoFile.Path
- End If
- Next fsoFile
- End Sub
复制代码 wav文件效果图:
示例及附件,供参考:
- Sub test()
- PlayTheSound "C:\Windows\media\Alarm04.wav"
- ' PlayTheSound Range("B5")
- ' PlayTheSound ActiveCell.Text
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- '运行指定程序
- '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|