About as simple as it can get, yet functional. Able to open long filenames and the filenames can contain Unicode charaters (of course you need to use file API to obtain such filenames).
Code:
'clsMCI.cls
Option Explicit
Public Event Error(ByVal Message As String)
Private Declare Function mciGetErrorStringW Lib "winmm" (ByVal dwError As Long, ByVal lpstrBuffer As Long, ByVal uLength As Long) As Long
Private Declare Function mciSendStringW Lib "winmm" (ByVal lpstrCommand As Long, Optional ByVal lpstrReturnString As Long, Optional ByVal uReturnLength As Long, Optional ByVal hwndCallback As Long) As Long
Private m_Error As String
Private m_ID As String
Private Sub MCI(Command As String, Optional Success As Boolean, Optional ByVal IgnoreError As Boolean)
Dim lngError As Long
lngError = mciSendStringW(StrPtr(Command & " " & m_ID))
If lngError Then
If Not IgnoreError Then
m_Error = Space$(260)
mciGetErrorStringW lngError, StrPtr(m_Error), 260
lngError = InStr(m_Error, vbNullChar)
If lngError Then m_Error = Left$(m_Error, lngError - 1)
RaiseEvent Error(m_Error)
End If
Else
Success = True
End If
End Sub
Public Function Continue() As Boolean
MCI "resume", Continue
End Function
Public Function Halt() As Boolean
MCI "stop", Halt
End Function
Public Function LastError() As String
LastError = m_Error
End Function
Public Function Load(Filename As String) As Boolean
MCI "close", , True
MCI "open """ & Filename & """ alias", Load
End Function
Public Function Pause() As Boolean
MCI "pause", Pause
End Function
Public Function Play() As Boolean
MCI "play", Play
End Function
Private Sub Class_Initialize()
m_ID = "mp3_" & Hex$(Timer * 1000)
End Sub
Private Sub Class_Terminate()
MCI "close", , True
End Sub
The following sample will open two files and plays both the same time. If there are errors, debug console will get messages for the first one.
Code:
' Form1.frm
Option Explicit
Private WithEvents MP3 As clsMCI
Private MP3b As clsMCI
Private Sub Form_Load()
Set MP3 = New clsMCI
If MP3.Load("C:\Users\YOU\Music\Test.mp3") Then MP3.Play
Set MP3b = New clsMCI
If MP3b.Load("C:\Users\YOU\Music\Test2.mp3") Then MP3b.Play
End Sub
Private Sub MP3_Error(ByVal Message As String)
Debug.Print Message
End Sub
FYI: Multimedia Command Strings