Winamp Class


Description:
This class provides a thin wrapper around many of the WM_WA_IPC functions. Use with caution.

7/12/1999: Added the command button messages.

12/10/2000: Added PressButton, which exposes more WinAmp functionality.

5/7/2004: Updated the Playlist functions to deal with changes in recent WinAmp versions.
 
Code:
'----------------------------------------------------------------------
' Start: clsWinamp

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) 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 PostMessage Lib "user32" Alias "PostMessageA" _
   (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
   lParam As Any) As Long
Private Declare Function PostMessageLng Lib "user32" Alias "PostMessageA" _
   (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
   ByVal lParam As Long) As Long

Private Const WM_COMMAND = &H111
Private Const WM_COPYDATA = &H4A

Private Const WINAMP_BUTTON1 = 40044
Private Const WINAMP_BUTTON2 = 40045
Private Const WINAMP_BUTTON3 = 40046
Private Const WINAMP_BUTTON4 = 40047
Private Const WINAMP_BUTTON5 = 40048

Private Const WM_USER = &H400
Private Const WM_WA_IPC = WM_USER

Private Const IPC_GETVERSION = 0
Private Const IPC_PLAYFILE = 100
Private Const IPC_DELETE = 101
Private Const IPC_STARTPLAY = 102
Private Const IPC_CHDIR = 103
Private Const IPC_ISPLAYING = 104
Private Const IPC_GETOUTPUTTIME = 105
Private Const IPC_JUMPTOTIME = 106
Private Const IPC_WRITEPLAYLIST = 120

Private Type COPYDATASTRUCT
    dwData As Long
    cbData As Long
    lpData As Long
End Type

Public Enum enumPlay
    playPlaying = 1
    playPaused = 3
    playStoped = 0
End Enum

Public Enum enumButtons
    PreviousTrackButton = 40044
    NextTrackButton = 40048
    PlayButton = 40045
    PauseUnpauseButton = 40046
    StopButton = 40047
    StopAfterCurrentTrack = 40147
    FadeoutAndStop = 40157
    FastForward5Seconds = 40148
    FastRewind5Seconds = 40144
    StartOfPlaylist = 40154
    GoToEndOfPlaylist = 40158
    OpenFileDialog = 40029
    OpenUrlDialog = 40155
    OpenFileInfoBox = 40188
    SetTimeDisplayModeToElapsed = 40037
    SetTimeDisplayModeToRemaining = 40038
    TogglePreferencesScreen = 40012
    OpenVisualizationOptions = 40190
    OpenVisualizationPlugInOptions = 40191
    ExecuteCurrentVisualizationPlugIn = 40192
    ToggleAboutBox = 40041
    ToggleTitleAutoscrolling = 40189
    ToggleAlwaysOnTop = 40019
    ToggleWindowshade = 40064
    TogglePlaylistWindowshade = 40266
    ToggleDoublesizeMode = 40165
    ToggleEq = 40036
    TogglePlaylistEditor = 40040
    ToggleMainWindowVisible = 40258
    ToggleMinibrowser = 40298
    ToggleEasymove = 40186
    RaiseVolumeBy1Perc = 40058
    LowerVolumeBy1Perc = 40059
    ToggleRepeat = 40022
    ToggleShuffle = 40023
    OpenJumpToTimeDialog = 40193
    OpenJumpToFileDialog = 40194
    OpenSkinSelector = 40219
    ConfigureCurrentVisualizationPlugIn = 40221
    ReloadTheCurrentSkin = 40291
    CloseWinamp = 40001
    MovesBack10TracksInPlaylist = 40197
    ShowTheEditBookmarks = 40320
    AddsCurrentTrackAsABookmark = 40321
    PlayAudioCd = 40323
    LoadAPresetFromEq = 40253
    SaveAPresetToEqf = 40254
    OpensLoadPresetsDialog = 40172
    OpensAutoLoadPresetsDialog = 40173
    LoadDefaultPreset = 40174
    OpensSavePresetDialog = 40175
    OpensAutoLoadSavePreset = 40176
    OpensDeletePresetDialog = 40178
    OpensDeleteAnAutoLoadPresetDialog = 40180
End Enum

Private m_sWinampDir As String
Private m_cPlaylist As Collection

Private Function WAWin() As Long
    WAWin = FindWindow("Winamp v1.x", vbNullString)
    Debug.Assert WAWin <> 0
End Function

Public Property Get Version() As Long
    Version = SendMessage(WAWin, WM_WA_IPC, 0, ByVal IPC_GETVERSION)
End Property

Public Sub AddPlaylist(ByVal sFile As String)

    Dim hWnd As Long
    hWnd = WAWin
    
    If Version > &H1700 Then
        Dim cds As COPYDATASTRUCT
        Dim sFileAscii As String
        sFileAscii = StrConv(sFile, vbFromUnicode)
        cds.dwData = IPC_PLAYFILE
        cds.lpData = StrPtr(sFileAscii)
        cds.cbData = Len(sFile) + 1
        SendMessage hWnd, WM_COPYDATA, 0, ByVal VarPtr(cds)
    Else
        sFile = sFile & vbNullChar
        While sFile <> ""
            PostMessageLng hWnd, WM_WA_IPC, Asc(Mid(sFile, 1, 1)), IPC_PLAYFILE
            sFile = Mid(sFile, 2)
        Wend
    End If
    
End Sub

Private Sub AddPlaylist2(Filename As String)
End Sub

Public Sub DeletePlaylist()
    PostMessage WAWin, WM_WA_IPC, 0, ByVal IPC_DELETE
End Sub

Public Sub StartPlay()
    PostMessage WAWin, WM_WA_IPC, 0, ByVal IPC_STARTPLAY
End Sub

Public Sub ChDir(ByVal sDir As String)

    Dim hWnd As Long
    hWnd = WAWin
    sDir = sDir & vbNullChar
    
    While sDir <> ""
        PostMessage hWnd, WM_WA_IPC, Asc(Mid(sDir, 1, 1)), _
            ByVal IPC_CHDIR
            
        sDir = Mid(sDir, 2)
    Wend

End Sub

Public Property Get IsPlaying() As enumPlay
    IsPlaying = SendMessage(WAWin, WM_WA_IPC, 0, ByVal IPC_ISPLAYING)
End Property

Public Property Get SongPos() As Long
    SongPos = SendMessage(WAWin, WM_WA_IPC, 0, ByVal IPC_GETOUTPUTTIME)
End Property

Public Property Get SongLen() As Long
    SongLen = SendMessage(WAWin, WM_WA_IPC, 1, ByVal IPC_GETOUTPUTTIME)
End Property

Public Sub JumpToTime(ByVal nTime As Long)
    PostMessage WAWin, WM_WA_IPC, nTime, ByVal IPC_JUMPTOTIME
End Sub

Public Property Get WinampDir() As String
    WinampDir = m_sWinampDir
End Property

Public Property Let WinampDir(sNew As String)
    m_sWinampDir = sNew
End Property

Public Function RefreshPlaylist() As Long
    RefreshPlaylist = SendMessage(WAWin, WM_WA_IPC, 0, _
                       ByVal IPC_WRITEPLAYLIST)
    
    Dim nFile As Byte
    Dim sLine As String
    nFile = FreeFile
    Open m_sWinampDir & "\winamp.m3u" For Input As #nFile
    
    Do While m_cPlaylist.Count > 0
        m_cPlaylist.Remove 1
    Loop
    
    While Not EOF(nFile)
        Line Input #nFile, sLine
        m_cPlaylist.Add sLine
    Wend
    
    Close #nFile
    
End Function

Public Function PlaylistCount() As Long
    PlaylistCount = m_cPlaylist.Count
End Function

Public Function PlaylistSong(nIndex As Long) As String
    Debug.Assert nIndex >= 1
    Debug.Assert nIndex <= m_cPlaylist.Count
    PlaylistSong = m_cPlaylist(nIndex)
End Function

Private Sub Class_Initialize()
    Set m_cPlaylist = New Collection
    m_sWinampDir = "c:\program files\winamp\"
End Sub

Public Sub CommandPrevSong()
    SendMessage WAWin, WM_COMMAND, WINAMP_BUTTON1, ByVal 0
End Sub

Public Sub CommandPlay()
    SendMessage WAWin, WM_COMMAND, WINAMP_BUTTON2, ByVal 0
End Sub

Public Sub CommandPause()
    SendMessage WAWin, WM_COMMAND, WINAMP_BUTTON3, ByVal 0
End Sub

Public Sub CommandStop()
    SendMessage WAWin, WM_COMMAND, WINAMP_BUTTON4, ByVal 0
End Sub

Public Sub CommandNextSong()
    SendMessage WAWin, WM_COMMAND, WINAMP_BUTTON5, ByVal 0
End Sub

Public Sub PressButton(buttonNum As enumButtons)
    SendMessage WAWin, WM_COMMAND, buttonNum, ByVal 0
End Sub

' End: clsWinamp
'----------------------------------------------------------------------
 
Sample Usage:
 
    Dim WA As New clsWinamp
    Dim nSong As Long
    
    Debug.Print "Winamp version: " & Hex(WA.Version)
    WA.AddPlaylist "C:\Mp3's\Superman.mp3"
    WA.StartPlay
    nSong = WA.RefreshPlaylist
    Debug.Print "Current Song: " & WA.PlaylistSong(nSong)