[英]Play any audio file using VBA Excel
我有一段代码可以读取大多数音频文件(包括 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)
我找到了解决方法,即文件名的路径名(和(编辑)中的正确空格(使用没有空格的文件副本,丑陋但有效)( name as
不是一个好的解决方案):
第一次尝试播放声音后,如果失败,我将当前目录更改为声音目录(暂时):
If Play <> 0 Then
Dim path$, FileName0$
path = CurDir
If InStr(sMusicFile, ":") > 0 Then ChDrive (Left(sMusicFile, 1))
If InStr(sMusicFile, "\") > 0 Then
ChDir (Left(sMusicFile, InStrRev(sMusicFile, "\") - 1))
FileName0 = Mid(sMusicFile, InStrRev(sMusicFile, "\") + 1)
If InStr(FileName0, " ") > 0 Then
FileCopy FileName0, Replace(FileName0, " ", "")
sMusicFile = Left(sMusicFile, InStrRev(sMusicFile, "\")) & Replace(FileName0, " ", "")
Play = mciSendString("play " & Replace(FileName0, " ", ""), 0&, 0, 0)
Else
Play = mciSendString("play " & FileName0, 0&, 0, 0)
End If
Else
FileName0 = Replace(sMusicFile, " ", "")
If sMusicFile <> FileName0 Then
FileCopy sMusicFile, FileName0
sMusicFile = FileName0
End If
Play = mciSendString("play " & sMusicFile, 0&, 0, 0)
End If
ChDrive (Left(path, 1))
ChDir (Left(path, InStrRev(path, "\") - 1))
End If
注意:对于名称中的空格,我还有一个新方法: Filecopy sMusicFile replace(sMusicFile," ","%")
然后播放这个新文件
去老派......想想DOS。
例如:
"C:\\Way Too Long\\Long Directory\\File.mp3"
变成
"C:\\WayToo~1\\LongDi~1\\File.mp3"
诀窍是去掉空格并将目录和文件名保持在 8 个字符以内。 为此,请删除所有空格,然后在前 6 个字符后截断并添加波浪号 (~) 和数字 1。
我试过这种方法,它对我来说非常有效。
需要注意的一件事是,如果缩短的目录名称(如“\\Long File Path\\”和“\\Long File Paths\\”和“\\Long File Path 1436\\”)中存在歧义的可能性,那么您需要调整波浪号后面的数字(“\\LongFi~1\\”和“\\LongFi~2\\”和“\\LongFi~3\\”,按照目录的创建顺序)。
因此,前一个文件夹可能被称为“FilePa~1”并被删除,而留下一个类似名称的“FilePa~2”。 因此,您的文件路径可能不会自动以“~1”为后缀。 它可能是“~2”或更高,这取决于有多少类似命名的目录或文件名。
我觉得不可思议的是,dos 是在 35 年前发布的,而 VBA 程序员仍然不得不处理这个目录问题的恐龙!
尝试:
Public Sub Sound2(ByVal File$)
If InStr(1, File, " ") > 0 Then File = """" & File & """"
sMusicFile = File
...
如果有空格,这会将路径用引号括起来,这是某些 API 函数所必需的。
以下解决方案无需复制文件即可工作。
它将您的代码与来自 osknows 的代码合并在一起, 使用 Unicode 文件名的完整路径,其想法来自上述 Jared...
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 Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Private sMusicFile As String
Dim Play, a
Public Sub Sound2(ByVal File$)
sMusicFile = GetShortPath(File)
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
Public Function GetShortPath(ByVal strFileName As String) As String
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [email]KPDTeam@Allapi.net[/email]
Dim lngRes As Long, strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRes = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRes)
End Function
该函数将长完整文件名转换为 8.3 短格式。
Function get8_3FullFileName(ByVal sFullFileName As String) As String
Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
get8_3FullFileName = FSO.GetFile(sFullFileName).ShortPath
End Function
尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.