Controlling Winamp2/3/Sonique Programmatically






4.55/5 (8 votes)
Mar 12, 2003
1 min read

71341

788
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