Click here to Skip to main content
15,881,757 members
Articles / Desktop Programming / MFC
Article

Controlling Winamp2/3/Sonique Programmatically

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
11 Mar 20031 min read 70.9K   788   42   6
An article on controlling popular media players from external programs.

Introduction

This article shows a method of controlling Winamp 1.x-2.x, Winamp3, and Sonique 1.x, programmatically from external programs.

Background

A while back, I was a beta tester for a cool little utility called Codename: Dashboard. Basically it is a sidebar-like program that serves as a holder for sub-components.

Anyway, there was a great Windows Media Player component for it, but nothing for Winamp. After having less-than-stellar experience with MP3s and WMP 6.4, I have pretty much refused to use it as my media player. (I know… I hear WMP9 is better. Time will tell.) This prompted me to write a component for Winamp…

There’s only one problem: I cant find ANY documentation on controlling Winamp3 remotely. It seems that the Nullsoft guys left out this capability in Wasabi. So after a bit of digging, here is what I came up with.

Using the code

First off, we have to figure out what player we are running:

HWND FindPlayer(int wanted) 
{
    HWND hwnd;

    if (wanted == 0 || wanted == 4) {
        hwnd = FindWindow("Sonique Window Class", NULL);
        if (hwnd != 0) {
            playerID = 4;
            return hwnd;
        }
    }

    if (wanted == 0 || wanted == 3) {
        hwnd = FindWindow("Studio", NULL);
        if (hwnd != 0) {
            playerID = 3;
            return hwnd;
        }
    }

    if (wanted == 0 || wanted == 2) {
        hwnd = FindWindow("Winamp v1.x", NULL);
        if (hwnd != 0) {
            playerID = 2;
            return hwnd;
        }
    }

    playerID = 0;

    return 0;
}

Next we figure out which command to send:

// Winamp 2 defines
#define W2_PREV             40044
#define W2_PLAY             40045
#define W2_PAUSE            40046
#define W2_STOP             40047
#define W2_NEXT             40048
#define W2_FILEPLAY         40029 

#define W2_ISPLAYING        104
#define W2_OUTPUTTIME       105
#define W2_JUMPTOTIME       106
#define W2_SETPLAYLISTPOS   121
#define W2_GETLISTLENGTH    124
#define W2_GETLISTPOS       125
#define W2_GETINFO          126


// Winamp 3 defines
#define W3_PREV                   'z'
#define W3_PLAY                   'x'
#define W3_PAUSE                   'c'
#define W3_STOP                   'v'
#define W3_NEXT                   'b'
#define W3_FILEPLAY               'l'


// Sonique 1 defines
#define S1_PREV                  'z'
#define S1_PLAY                 'x'
#define S1_PAUSE                 'c'
#define S1_STOP                 'v'
#define S1_NEXT            'b'
#define S1_FILEPLAY        'l'

Then we send the command to the proper window:

void SendW3Key(HWND hwnd_winamp,char message)
{
    short key = VkKeyScan(message);
    UINT scancode = MapVirtualKey(key, 0);
    PostMessage(hwnd_winamp, WM_KEYDOWN, key, scancode);
    PostMessage(hwnd_winamp, WM_CHAR, key, scancode);
    PostMessage(hwnd_winamp, WM_KEYUP, key, scancode);
}

So to bring it all together, it looks something like this:

private void AmpControl_Play(void)
{
HWND hwnd_winamp = FindPlayer(0);

    if (hwnd_winamp != 0) {
        if (playerID == 2) {
            SendMessage(hwnd_winamp, WM_COMMAND, W2_PLAY, 0);
        }
        else if (playerID == 3) {
            SendW3Key(hwnd_winamp, W3_PLAY);
        }
        else if (playerID == 4) {
            SendW3Key(hwnd_winamp, S1_PLAY);
        }
    }
}

The source includes Play, Pause, Next, Previous, Open and Stop. All packaged in an easy-to-use DLL project.

History

v1.0.0.0 - 11 Mar 2003 - Initial Release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I'm a 23 year old student/.NET Developer from Charlotte NC. I spend my free time with my wonderful wife and 2 cats. At least I would if I had any free time...

Comments and Discussions

 
GeneralMore Winamp commands Pin
AndrewSmirnov17-May-04 8:28
AndrewSmirnov17-May-04 8:28 
GeneralPlugin needed for using WM_COMMAND, WM_USER and WM_COPYDATA stuff Pin
Lars [Large] Werner12-May-03 0:09
professionalLars [Large] Werner12-May-03 0:09 
GeneralPretty Cool Idea... Pin
Joel Holdsworth12-Mar-03 10:32
Joel Holdsworth12-Mar-03 10:32 
GeneralRe: Pretty Cool Idea... Pin
Trollslayer11-Aug-05 10:51
mentorTrollslayer11-Aug-05 10:51 
GeneralRe: Pretty Cool Idea... Pin
Joel Holdsworth11-Aug-05 10:58
Joel Holdsworth11-Aug-05 10:58 
GeneralRe: Pretty Cool Idea... Pin
Trollslayer11-Aug-05 21:57
mentorTrollslayer11-Aug-05 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.