Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Winamp Lock & Pause


  • Please log in to reply
6 replies to this topic
mrinferno
  • Members
  • 10 posts
  • Last active: Apr 13 2010 02:17 PM
  • Joined: 21 Apr 2006
Winamp Lock & Pause is a script I've created that's basically designed to Pause Winamp whenever you manually Lock your WinXP workstation (typically by hitting Win+L or Ctrl+Alt+Del, Lock Computer). When you unlock your workstation Winamp will start playing again (by design, even if Winamp was already Paused when you locked). If Winamp is stopped or not open, it does nothing.

I realize there are a few different Winamp plugins out there that will do things similar to this. I've tried most of them and to me they were inadequate.

I'm posting this script because I really hope someone else finds it useful. I love AutoHotkey and I wanted to give back a little to the community. I use this script everyday on my work laptop. I'm not a programmer, but it works so I'm happy. I'm sure there are optimizations that could be done and please by all means post any tweaks or changes you think would be beneficial.

Here are a couple other things to note before I post the code:
-It does work whether or not you have Winamp set to minimize to the taskbar or to a systray icon.
-It's really intended to be used only for manually Locking/Unlocking your workstation. It doesn't work right if you have the ScreenSaver configured to "On resume, password protect" because the workstation doesn't actually enter a locked state until the ScreenSaver turns off.
-It's meant to run as a resident script in your AutoHotkey.ini. Although, I did test it as a stand alone script and it could be modified back to that state.
-I tried to put good, meaningful comments throughout to help people who want to take the code and make it their own.
-I give credits in my comments to 2 people from the AHK forums, "shimanov" & "deadprez". Both of whom had some good code that I used in creating my script. Many thanks to both of them!!!

And lastly, I give you Winamp Lock & Pause v0.6:

Here's the code to add into your AutoHotkey.ini
(assumes you have "winamp_Lock_Pause v0.6.ahk" in the AutoHotkey install dir)
;Launch Winamp_Lock_Pause as resident script
#include %A_ScriptDir%\winamp_Lock_Pause v0.6.ahk
Winamp_Lock_Pause()

Here is the main code, copy & paste to a text file "winamp_Lock_Pause v0.6.ahk"
; --------------------------------------------------------------- 
; Winamp_Lock_Pause
;	This script monitors LockWorkstation calls
;
;	First checks to see if Winamp is running
;		On Lock
;			If Winamp is playing a song, pause playback
;			Else If Winamp is paused, do nothing
;			Else If Winamp is stopped, do nothing
;		On Unlock
;			If Winamp is paused, restart playback
;			Else If Winamp is stopped, do nothing
; ---------------------------------------------------------------
; Largely created by MrInferno
; Last edited 2006-04-07 20:38
; ---------------------------------------------------------------
; Based on script codes from "shimanov"
; Posted: Thu Sep 15, 2005 12:26 am   
; Source: http://www.autohotkey.com/forum/viewtopic.php?t=5359
; Posted: Tue Dec 06, 2005 9:14 pm
; Source: http://www.autohotkey.com/forum/viewtopic.php?t=6755
; ---------------------------------------------------------------

;Initialize global constants
WTS_SESSION_LOCK			:=	0x7
WTS_SESSION_UNLOCK			:=	0x8
NOTIFY_FOR_ALL_SESSIONS		:=	1
NOTIFY_FOR_THIS_SESSION		:=	0
WM_WTSSESSION_CHANGE		:=	0x02B1
	
Winamp_Lock_Pause( )
{
	Global WM_WTSSESSION_CHANGE
	Global NOTIFY_FOR_ALL_SESSION
	
	hw_ahk := FindWindowEx( 0, 0, "AutoHotkey", a_ScriptFullPath " - AutoHotkey v" a_AhkVersion ) 

	OnMessage( WM_WTSSESSION_CHANGE, "Handle_WTSSESSION_CHANGE" ) 
	
	success := DllCall( "wtsapi32.dll\WTSRegisterSessionNotification", "uint", hw_ahk, "uint", NOTIFY_FOR_ALL_SESSIONS )
	  
	if( ErrorLevel OR ! success ) 
	{ 
	   success := DllCall( "wtsapi32.dll\WTSUnRegisterSessionNotification", "uint", hw_ahk )
	   ;If DLL registration fails, wait 20 seconds and try again
	   Sleep, 20000
	   Winamp_Lock_Pause( )
	   ;MsgBox, [WTSRegisterSessionNotification] failed: EL = %ErrorLevel%
	} 
	return

;End Winamp_Lock_Pause
} 

Handle_WTSSESSION_CHANGE( p_w, p_l, p_m, p_hw )
; p_w  = wParam	;Session state change event
; p_l  = lParam	;Session ID
; p_m  = Msg	;WM_WTSSESSION_CHANGE
; p_hw = hWnd	;Handle to Window
{ 
	Global WTS_SESSION_LOCK
	Global WTS_SESSION_UNLOCK
	
	;Is Winamp is running
	DetectHiddenWindows, On		;functions here work better with this option
	SetTitleMatchMode, 2
	Process, Exist, Winamp.exe
	NewPID := ErrorLevel
	;Errorlevel will be set to winamp.exe's PID (i.e. not blank or zero) if it's running
	If NewPID ;not blank or zero value
	{
		If ( p_w = WTS_SESSION_LOCK ) 
		{
			Winamp_Pause( )
		}
		Else If ( p_w = WTS_SESSION_UNLOCK ) 
   		{
   			Winamp_Play( )
   		}
	}
	;If Winamp isn't running, do nothing
	
	DetectHiddenWindows, Off	
} 

FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title ) 
{ 
   return, DllCall( "FindWindowEx", "uint", p_hw_parent, "uint", p_hw_child, "str", p_class, "str", p_title ) 
} 

; --------------------------------------------------------------- 
; Winamp_Pause
;		Assumes already checked for Winamp process
;   	If Winamp is playing a song, pause playback
;		Else If Winamp is paused, do nothing
;		Else If Winamp is stopped, do nothing
; ---------------------------------------------------------------
; Largely created by MrInferno
; Last edited 2006-04-06 18:53
; ---------------------------------------------------------------
; Based on script code from "deadprez"
; Posted: Tue Oct 04, 2005 2:28 pm  
; Source:  http://www.autohotkey.com/forum/viewtopic.php?t=5667
; ---------------------------------------------------------------
Winamp_Pause( )
{
	;Initialize constants
	WM_USER			= 0x0400 
	WM_COMMAND		= 0x0111 
	IPC_ISPLAYING	= 104 
	WINAMP_BUTTON3	= 40046		;Pause button 
	
	;Is Winamp currently playing a song? 
	SendMessage, WM_USER, 0, IPC_ISPLAYING,, ahk_class Winamp v1.x
	WinampStatus := ErrorLevel 
	if (WinampStatus == 1)
	{ 
		;Winamp is playing, pause playback
		SendMessage, WM_COMMAND, WINAMP_BUTTON3, WINAMP_BUTTON3,, ahk_class Winamp v1.x
		Return 
	}	
	else if (WinampStatus == 3 )
	{ 
		;Winamp is paused, do nothing
		Return 
	}
	else
	{
		;Winamp is stopped, do nothing
		Return
	}
;End Winamp_Pause
}

; --------------------------------------------------------------- 
; Winamp_Play
;		Assumes already checked for Winamp process
;		If Winamp is paused, restart playback
;		Else If Winamp is stopped, do nothing
; ---------------------------------------------------------------
; Largely created by MrInferno
; Last edited 2006-04-06 18:53
; ---------------------------------------------------------------
; Based on script code from "deadprez"
; Posted: Tue Oct 04, 2005 2:28 pm  
; Source:  http://www.autohotkey.com/forum/viewtopic.php?t=5667
; ---------------------------------------------------------------
Winamp_Play( )
{
	;Initialize constants
	WM_USER			= 0x0400 
	WM_COMMAND		= 0x0111
	IPC_ISPLAYING	= 104 
	WINAMP_BUTTON3	= 40046		;Pause button
	 
	;Is Winamp currently playing a song? 
	SendMessage, WM_USER, 0, IPC_ISPLAYING,, ahk_class Winamp v1.x
	WinampStatus := ErrorLevel 
	if (WinampStatus == 1)
	{ 
		;Winamp is playing, do nothing
		Return 
	}	
	else if (WinampStatus == 3 )
	{ 
		;Winamp is paused, restart playback
		SendMessage, WM_COMMAND, WINAMP_BUTTON3, WINAMP_BUTTON3,, ahk_class Winamp v1.x
		Return 
	}
	else
	{
		;Winamp is stopped, do nothing
		Return
	}
;End Winamp_Play
}


T-nm
  • Guests
  • Last active:
  • Joined: --
You can also use the Global Hotkey function implemented in Winamp; you can pause, fast forward, etc..
I also encountered a script that when you Ctrl+F12 you type something like Bowie Jean (it will open David Bowie - Blue Jean.mp3) and then press Enter to play it or add it to the playlist. Very cool...

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
I know this is a little old, but still an excellent script.
I am specifically interested in the pause/play functions.

Up until now, I simply sent z, x, c, v or b to winamp, but this is more reliable, since the x key is a toggle.

thanks for this.

mrinferno
  • Members
  • 10 posts
  • Last active: Apr 13 2010 02:17 PM
  • Joined: 21 Apr 2006
no problem, glad somebody else found it useful.

i've updated the code above to v0.6 since i last posted this, for anyone else interested.

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Excellent indeed. I used your Play/Pause routines in my script, and it works like a charm.

Perhaps you should create an include library with winamp functions - I would have done it myself, if I understood half of what you coded... :)

Nice to also see a well documented and neatly indented code for a change.

Good functions for such library:
IsWinampExist, IsWinampPlaying, IsWinampPaused, WinampPlay, WinampPause, WinampStop, WinampNext, WinampPrev etc.

Say, while we are on the subject - would you mind posting a reference to where you learned how to communicate with winamp in such an intimate level? Is this a part of Winamp's API?

I considered - instead of pausing/replaying - to just reduce the volume to lets say, 20%, and restore it to where it was (so I will probably need to code functions - WinampSetVolume( pct ) and pct := WinampGetVolume() ).

mrinferno
  • Members
  • 10 posts
  • Last active: Apr 13 2010 02:17 PM
  • Joined: 21 Apr 2006
as i said in my comments to the code above, i used part of the code from several different scripts. then just tweaked & optimized until i got what i needed.

additionally i did use the Winamp SDK package to get the Winamp API info to code some of it. The file that was most helpful was ".\winamp\wa_ipc.h".

The Winamp SDK documentation definitely where you can find the info to create specific SendMessage commands. I'm not much of a programmer so it was hard to dig through it, but not impossible :wink:

if you haven't already, check out the Catalog of All Scripts for more examples. when i started out, i looked at all the scripts with winamp in the title til i found what i was looking for.

Icarus
  • Members
  • 851 posts
  • Last active: Jan 02 2012 11:17 AM
  • Joined: 24 Nov 2005
Thanks.
I also looked for all "Winamp" scripts, until I found this thread :)