jeudi 1 janvier 2015

Play any audio file using vba excel

and happy New Year.


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.


so i have to revert to my other code wich accepts it, but read only wav files...


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"

Err.Clear
On Error Resume Next

Play = mciSendString("play " & sMusicFile, 0&, 0, 0)
If Play <> 0 Or Err.Number <> 0 Then 'this triggers if can't play the file
Err.Clear
'Play = mciSendString("'play " & sMusicFile & "'", 0&, 0, 0) 'i tried this aproach, but doesn't seem to work
End If
Err.Clear
On Error GoTo 0
End Sub


Public Sub StopSound(Optional ByVal FullFile$)
Play = mciSendString("close " & sMusicFile, 0&, 0, 0)
End Sub


Any help much appreciated, (i don't want workaround with external player popup, nor wich i can't stop from playing with vba)


Aucun commentaire:

Enregistrer un commentaire