Could you play mp3 files with the Windows api ?
(just like wave-files)
Thanks
Printable View
Could you play mp3 files with the Windows api ?
(just like wave-files)
Thanks
Try this code
Code:oh definitely, Use the following code
Private Declare 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 mciGetErrorString Lib "winmm.dll" Alias _
"mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer _
As String, ByVal uLength 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
Public Sub Play(CFileName As String)
Dim strMusicErr As String * 128
Dim mciCommand As String
Dim strReturn As String
Dim lngRV as long
'get the short path
cFileName = GetShortPath(cFileName)
'open the music file
mciCommand = "open " & CFileName & " type MPEGVideo alias MP3Play"
lngRV = mciSendString(mciCommand, 0&, 0&, 0&)
If lngRV <> 0 Then
lngRV = mciGetErrorString(lngRV, strMusicErr, 128)
MsgBox strMusicErr, 16, "Error opening Music file"
Exit Sub
End If
'play the music file
lngRV = mciSendString("play Mp3Play Notify", strReturn, Len(strReturn), cmdHidden.hWnd)
If lngRV <> 0 Then
lngRV = mciGetErrorString(lngRV, strMusicErr, 128)
MsgBox strMusicErr, 16, "Error playing Music"
Exit Sub
End If
End Sub
Public Function GetShortPath(strFileName As String) As String
Dim lngRV As Long
Dim strPath As String
'Create a buffer
strPath = String$(165, 0)
'retrieve the short pathname
lngRV = GetShortPathName(strFileName, strPath, 164)
'remove all unnecessary chr$(0)'s
GetShortPath = Left$(strPath, lngRV)
End Function
To make a complete mp3 player, use this code.
Code:Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Long, ByVal lpstrBuffer As String, ByVal uLength As Long) As Long
Private Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private TheFileName As String
Private Function IsItPlaying() As Boolean
Static yn As String * 30
mciSendString "status mp3play mode", yn, Len(yn), 0
IsItPlaying = (Mid$(yn, 1, 7) = "playing")
End Function
Private Function mp3play(FileName As String)
Dim cmdToDo As String * 255
Dim dwReturn As Long
Dim ret As String * 128
Dim tmp As String * 255
Dim lenShort As Long
Dim ShortPathAndFie As String
If Dir(FileName) = "" Then
mmOpen = "Error with input file"
Exit Function
End If
lenShort = GetShortPathName(FileName, tmp, 255)
ShortPathAndFie = Left$(tmp, lenShort)
glo_hWnd = hwnd
cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias mp3play"
dwReturn = mciSendString(cmdToDo, 0&, 0&, 0&)
If dwReturn <> 0 Then 'not success
mciGetErrorString dwReturn, ret, 128
mmOpen = ret
MsgBox ret, vbCritical
Exit Function
End If
mmOpen = "Success"
mciSendString "play mp3play", 0, 0, 0
End Function
Public Function mp3Pause()
mciSendString "pause mp3play", 0, 0, 0
End Function
Public Function mp3Unpause()
mciSendString "resume mp3play", 0, 0, 0
End Function
Public Function mp3Stop() As String
mciSendString "stop mp3play", 0, 0, 0
mciSendString "close mp3play", 0, 0, 0
End Function
Public Function PositionInSec()
Static PIS As String * 30
mciSendString "set mp3play time format milliseconds", 0, 0, 0
mciSendString "status mp3play position", PIS, Len(PIS), 0
PositionInSec = Round(Mid$(PIS, 1, Len(PIS)) / 1000)
End Function
Public Function Position()
Static P As String * 30
mciSendString "set mp3play time format milliseconds", 0, 0, 0
mciSendString "status mp3play position", P, Len(P), 0
sec = Round(Mid$(P, 1, Len(P)) / 1000)
If sec < 60 Then Position = "0:" & Format(sec, "00")
If sec > 59 Then
mins = Int(sec / 60)
sec = sec - (mins * 60)
Position = Format(mins, "00") & ":" & Format(sec, "00")
End If
End Function
Public Function LengthInSec()
Static L As String * 30
mciSendString "set mp3play time format milliseconds", 0, 0, 0
mciSendString "status mp3play length", L, Len(s), 0
LengthInSec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(s, 1, Len(s))) / 1000)
End Function
Public Function Length()
Static L As String * 30
mciSendString "set mp3play time format milliseconds", 0, 0, 0
mciSendString "status mp3play length", L, Len(L), 0
sec = Round(Val(Mid$(L, 1, Len(L))) / 1000) 'Round(CInt(Mid$(l, 1, Len(l))) / 1000)
If sec < 60 Then Length = "0:" & Format(sec, "00")
If sec > 59 Then
mins = Int(sec / 60)
sec = sec - (mins * 60)
Length = Format(mins, "00") & ":" & Format(sec, "00")
End If
End Function
Public Function SeekTo(Second)
mciSendString "set mp3play time format milliseconds", 0, 0, 0
If IsItPlaying = True Then mciSendString "play mp3play from " & Second, 0, 0, 0
If IsItPlaying = False Then mciSendString "seek mp3play to " & Second, 0, 0, 0
End Function
Hi Mathew,
I've got this MP3 API a long ago. Now What the Proble I'm having is while Playing I'm unable to Pause the Mp3 file. What Should I add to Pause and Resume the Playing MP3.
Ramanan.
Did you use the mp3pause and mp3unpause functions from above?
Hi Mathew Gates,
I didn't see that and It's working for me. I Have one doubt. I tried the Impulse Studio MP3 control. It's was great. But the Control which is used to Get the Visualtions (Ghost Canvas) is a Trial Version. I tried to get some Visualization with the GetwaveOutCaps() and other Mixer controls. But they are sluggish even when the Timer interval is set to 1. I'm having P!!! 650 MHz, nVidia AGP with 32MB Ram. So I'm trying to find a different way. are there any alternates. I need to get a Visualization that is Unique to My Player. also it should be very fast like the One in Winamp, Windows Media Player, Real Player, JetAudio, etc., Is it Possible to make Equalizers of our own Frequencies with API or any other way (Not a OCX like Impulse MP3 Mixer) like the one in Winamp and WMP. If you've any idea, Please help me... Advanced Thanks....
:D