播放使用VBA Excel的任何音频文件 [英] Play any audio file using vba excel
问题描述
和新年快乐。
我现在有一张code至极可以读取大多数的音频文件(包括WAV,MP3,MIDI ...),但如果有路径中的空格或它不会工作的文件名。
I have currently a piece of code wich can read most audio files (including wav, mp3, midi...), but it wont work if there are spaces in the path or File name.
所以我必须恢复到我的其他code至极接受它,但只读WAV文件...
so i have to revert to my other code wich accepts it, but read only wav files...
这是个code读取所有类型的音频:
this is th code for reading all type of audio :
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
任何帮助非常AP preciated,(我不想与外部播放器弹出的解决方法,也至极我不能从VBA停止播放)
Any help much appreciated, (i don't want workaround with external player popup, nor wich i can't stop from playing with vba)
推荐答案
我找到了解决方法,路径名称((编辑)的文件名称正确的空格(使用文件的副本没有空格的,丑陋的,但作品(名称
将不会是一个很好的解决方案):
i found The work-around, that correct spaces in path name (and (edit) for file name (using copy of file with no spaces, ugly but works (name as
would not be a good solution) :
播放声音,如果失败,我改变当前目录声音目录中的第一次尝试后(temporarely):
After the first attempt to play the sound, if fails i change the current directory to the sound directory (temporarely):
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替换(sMusicFile,,%)
,然后播放这个新的文件
Note : for spaces in the name i got also a new method : Filecopy sMusicFile replace(sMusicFile," ","%")
and then play this new file
这篇关于播放使用VBA Excel的任何音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!