和欢乐的新年播放任何音频文件。使用VBA擅长
我目前的一块代码至极可以读取大多数的音频文件(包括WAV,MP3,MIDI ...),但如果有在路径中有空格或它不会工作的文件名。
所以我必须回到我的其他代码至极接受它,但只读WAV文件...
这是个代码读取所有类型的音频:
Option Explicit
Private Declare PtrSafe 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
Private sMusicFile As String
Dim Play
Public Sub Sound2(ByVal File$)
sMusicFile = File 'path has been included. Ex. "C:\3rdMan.mp3
Play = mciSendString("play " & sMusicFile, 0&, 0, 0)
If Play <> 0 Then 'this triggers if can't play the file
'Play = mciSendString("'play " & sMusicFile & "'", 0&, 0, 0) 'i tried this aproach, but doesn't seem to work
End If
End Sub
Public Sub StopSound(Optional ByVal FullFile$)
Play = mciSendString("close " & sMusicFile, 0&, 0, 0)
End Sub
任何帮助非常赞赏,(我不希望与外部播放器弹出的解决方法,也不是我不能停止使用vba)
你是什么意思“这将无法正常工作”?你有错误吗? (可能不会,因为你使用'On Error Resume Next'语句)。找出引发的具体错误,并且可能会更容易为您提供帮助。这将需要至少暂时摆脱“On Error Resume Next”。 –
为什么要播放音频?我是那种讨厌这种功能的用户之一。 –
它不会引发错误,但是如果sMusicFile包含空格,它将根本无法播放... –