VBA关于声音的多种实现方法
作者:互联网
1 在EXCEL里播放声音
1
- 如果是64位系统,需要加 PtrSafe 关键字,否则不需要
- Private Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal uFlags As Long) As Long
- sndPlaySound32(文件参数) 参数这写声音文件的绝对路径就行
Private Declare PtrSafe Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszName As String, ByVal uFlags As Long) As Long
Sub test1()
Call sndPlaySound32("F:\cowork\俄罗斯方块\true2.wav", 0&) '这里写声音文件的绝对路径就行
End Sub
2 在窗体里播放声音和视频文件
2.1 在窗体里用 windowsMediaPlayer的播放器,播放声音,甚至视频?
- 创建窗体
- 增加windowsMediaPlayer的播放器
- 增加控制按钮,给窗体里增加代码
- 然后,运行窗体
- 点击按钮
2.2 如何增加windowsMediaPlayer的播放器
2.3 代码
- 其中WindowsMediaPlayer1是控件的名称
- 关键代码
- WindowsMediaPlayer1.URL = path1 & "true2.wav"
- 需要是绝对路径,但也可以取得文件 path 再组合相对路径称为 可支持移动 的绝对路径,将表格和资源打包在一起。
- WindowsMediaPlayer1.Controls.Play
- 控制播放
Private Sub CommandButton1_Click()
path1 = ThisWorkbook.Path & "\"
WindowsMediaPlayer1.URL = path1 & "true2.wav"
WindowsMediaPlayer1.Controls.Play
End Sub
Private Sub WindowsMediaPlayer1_OpenStateChange(ByVal NewState As Long)
End Sub
Private Sub File1_Click()
WindowsMediaPlayer1.url = App.Path + "\" + File1.List(File1.ListIndex)
WindowsMediaPlayer1.Controls.play
End Sub
Private Sub Form_Load()
File1.Path = App.Path
End Sub
Private Sub WindowsMediaPlayer1_OpenStateChange(ByVal NewState As Long)
WindowsMediaPlayer1.url = App.Path + "\1.mp3"
WindowsMediaPlayer1.Controls.play
'WindowsMediaPlayer1.controls.pause '暂停
'Controls.currentPosition = 10 '设置bai当前播du放未知zhi
End Sub
播放多个和播放下一曲?
WindowsMediaPlayer1.close
Private Sub UserForm_Click() play1.Visible = False arr = array("xiao3.mp3","xue3.mp3","sheng1.mp3") for each p in arr play1.URL = p play1.Controls.Play Sleep 2000 next End Sub |
2.4 甚至可以隐藏 播放器,但一样可以控制它
标签:多种,VBA,End,Sub,声音,WindowsMediaPlayer1,ByVal,Private,Controls 来源: https://blog.csdn.net/xuemanqianshan/article/details/113485233