Jump to content

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

Can AHK get "play state" of Windows Media Player?


  • Please log in to reply
8 replies to this topic
Ron1
  • Members
  • 42 posts
  • Last active: Apr 01 2014 06:58 PM
  • Joined: 20 Dec 2007
Using, as a model, Lexikos excellent script on playing a specific track on a CD-ROM in Windows Media Player 11, I am making a second effort to retrieve the play state of WMP 11 for another purpose, but still unsuccessfully. Any thoughts on where I am going wrong would be gratefully received.

I always get 0 [= undefined] for the play state, no matter what the actual play state. Sean kindly suggested that the play state may only be retrievable if initially set with COM, so I attempted to do that first, which I unfortunately failed to do, so my test for a functioning play state thereafter is not meaningful.

In the first part of the following code, I have extracted the key commands from Lexikos model, and show that they work, to get the number of CD-ROM drives on the computer (1) and to get the number of tracks on the loaded CD (14), both of which are correct results. Then I go on to invoke a "property" called "playState" that the MSDN documentation (URL below) lists as parallel to the property "cdromCollection". Because of that parallel relationship, I use script parallel to that used with cdromCollection. But while cdromCollection works, playState does not work. [My thanks to "Guest" for pointing me to playState.]

I use the hot key P to trigger the routine, so that I can be in Windows Media Player 11, with a CD playing or not, when I hit the P.

P::
{
COM_Init()

; Get the number of CD-ROM drives in the computer

    wmp := COM_CreateObject("WMPlayer.OCX")					; WORKS (returns number > 0)
    cdroms := COM_Invoke(wmp, "cdromCollection")				; WORKS (returns number > 0)
    number_of_CD_ROM_drives := COM_Invoke(cdroms, "count")			; WORKS (returns 1, which is correct
										;       number of CD-ROM drives)

; Get the number of tracks on the CD-ROM currently playing

    which_CD_ROM = 0 ; the first (and only CD_ROM on this computer) is number 0
    cdrom := COM_Invoke(cdroms, "item", which_CD_ROM)				; WORKS (returns number > 0)
    playlist := COM_Invoke(cdrom, "playlist")					; WORKS (returns number > 0)
    tracks_on_CD := COM_Invoke(playlist, "count")				; WORKS (returns 14, which is correct
										;       number of tracks on CD)

; Get the play state of the Windows Media Player
; (0 = undefined, 1 = stopped, 2 = paused, 3 = playing, etc)

    playState := COM_Invoke(wmp, "playState")					; FAILS (returns 0 [= undefined],
										; no matter what the actual play state
										; but does NOT return a null) 		

			
; BEGIN SECOND EFFORT TO GET "playState" 
; by attemting to invoke "play" with COM first

; Check if control is available

    controls := COM_Invoke(wmp, "controls")					; WORKS (returns number > 0)
    isAvailable_play := COM_Invoke(controls, "isAvailable", "play" )		; FAILS (returns 0)
 
; Use COM to start playing

    play := COM_Invoke(controls, "play()")					; FAILS (returns null, which is okay
										; because this "method"
										; does not return a value)

; Get the play state of the Windows Media Player
; (0 = undefined, 1 = stopped, 2 = paused, 3 = playing, etc)

    playState_2 := COM_Invoke(wmp, "playState") 				; FAILS (returns 0 [= undefined],
										; no matter what the actual play state
										; but does NOT return a null)
MsgBox,
(
Get the number of CD-ROM drives in the computer
	wmp = %wmp%
	cdroms = %cdroms%
	number_of_CD_ROM_drives = %number_of_CD_ROM_drives%

Get the number of tracks on the CD-ROM currently playing
	cdrom = %cdrom%
	playlist = %playlist%
	tracks_on_CD = %tracks_on_CD%
 
Get the play state of the Windows Media Player
	playState = %playState%

BEGIN SECOND EFFORT to get valid "playState" 
by attemting to invoke "play" with COM first

Check if control is available
	controls = %controls%
	isAvailable_play = %isAvailable_play%

Use COM to start playing
	play = %play%

Get the play state of the Windows Media Player again
	playState_2 = %playState%
)

COM_Release(controls)
COM_Release(playlist)
COM_Release(cdroms)
COM_Release(cdrom)
COM_Release(wmp)

COM_Term()
}

return

ExitApp

Any thoughts on how to get playState working would be gratefully received.

REFERENCES:

Lexikos script to play a specific track on a CD-ROM drive and gather the track titles:
<!-- w -->www.autohotkey.com/forum/viewtopic.php?p=172534#172534<!-- w -->

MSDN documentation for "player" object:
<!-- m -->http://msdn.microsof...ry ... 85).aspx<!-- m -->

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I think there is some kind of glitch in phpBB that prevented the post above from being visible. I've edited its final URL a little and now the entire post is visible.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
What I meant was that you must start playing the CDRom from the created wmp. Please try again after replacing your first playState := COM_Invoke(wmp, "playState") with
COM_Invoke(wmp, "currentPlaylist", [color=red]"+"[/color] playlist) ; playlist is a COM object, so the prefix [color=red]+[/color] is required when used as a parameter.
controls := COM_Invoke(wmp, "controls")
COM_Invoke(controls, "play")
Sleep 3000
playState := COM_Invoke(wmp, "playState")


engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005

I think there is some kind of glitch in phpBB that prevented the post above from being visible. I've edited its final URL a little and now the entire post is visible.

I noticed the same thing in another topic when I was trying to fix a URL. Glad it wasn't just me.

Ron1
  • Members
  • 42 posts
  • Last active: Apr 01 2014 06:58 PM
  • Joined: 20 Dec 2007
Sean,

Thank you again for the effort to help. I am most grateful.

I modified my code, as you suggested, as shown below. But, unfortunately, I got the same result. That is, playState = 0 always.

Further, as the annotations in the code below indicate, the CD player did not start.

I noted, too, that if I placed "Result :=" in front of the following line

COM_Invoke(wmp, "currentPlaylist", "+" playlist)

to form

Result := COM_Invoke(wmp, "currentPlaylist", "+" playlist)

then Result returned a null. I am not sure if that is significant, since no return value from this COM_Invoke is utilized by the code.

P::
{
COM_Init()

; Get the number of CD-ROM drives in the computer

    wmp := COM_CreateObject("WMPlayer.OCX")					; WORKS (returns number > 0)
    cdroms := COM_Invoke(wmp, "cdromCollection")				; WORKS (returns number > 0)
    number_of_CD_ROM_drives := COM_Invoke(cdroms, "count")			; WORKS (returns 1, which is correct
										;       number of CD-ROM drives)

; Get the number of tracks on the CD-ROM currently playing

    which_CD_ROM = 0 ; the first (and only CD_ROM on this computer) is number 0
    cdrom := COM_Invoke(cdroms, "item", which_CD_ROM)				; WORKS (returns number > 0)
    playlist := COM_Invoke(cdrom, "playlist")					; WORKS (returns number > 0)
    tracks_on_CD := COM_Invoke(playlist, "count")				; WORKS (returns 14, which is correct
										;       number of tracks on CD)

; Get the play state of the Windows Media Player, after first starting it to play.
; (0 = undefined, 1 = stopped, 2 = paused, 3 = playing, etc)
; Insert script from Sean, and annotate with comments to the right

; =================
	
    COM_Invoke(wmp, "currentPlaylist", "+" playlist) ; playlist is a COM object, so the prefix + is required when used as a parameter.
										; When "Result :=" is placed in front of above line, a null is returned.
    controls := COM_Invoke(wmp, "controls")					; WORKS (returns number > 0)
    COM_Invoke(controls, "play")						; FAILS (player does not start playing loaded CD)
    Sleep 3000
    playState := COM_Invoke(wmp, "playState")					; FAILS (returns 0 [= undefined])

; ================

MsgBox,
(
Get the number of CD-ROM drives in the computer
	wmp = %wmp%
	cdroms = %cdroms%
	number_of_CD_ROM_drives = %number_of_CD_ROM_drives%

Get the number of tracks on the CD-ROM currently playing
	cdrom = %cdrom%
	playlist = %playlist%
	tracks_on_CD = %tracks_on_CD%
 
Get the play state of the Windows Media Player
	controls = %controls%
	playState = %playState%

)

COM_Release(controls)
COM_Release(playlist)
COM_Release(cdroms)
COM_Release(cdrom)
COM_Release(wmp)

COM_Term()
}

return

ExitApp

I am puzzled as to why Microsoft would require that the CD-ROM player be put in motion by commands like

COM_Invoke(controls, "play")

in order for the detection of playState to work.

Any thoughts would be appreciated.

Ron1

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

COM_Invoke(wmp, "currentPlaylist", "+" playlist)
then Result returned a null.

This used to be not allowed in old versions of COM.ahk. Please redownload the latest COM.ahk.

I am puzzled as to why Microsoft would require that the CD-ROM player be put in motion by commands like COM_Invoke(controls, "play") in order for the detection of playState to work.

It depends on your setting of autoStart of WMP. If you haven't changed it, probably you don't need to include it in the script.

Ron1
  • Members
  • 42 posts
  • Last active: Apr 01 2014 06:58 PM
  • Joined: 20 Dec 2007
Sean,

Thank you alerting me to the update needed for the COM Standard Library. That worked. I am most grateful for your help.

Two questions:

(1) Why is there a period (.) after the "+" in your example
COM_Invoke(Object, "Function", "+" . ObjPrm)on the COM Standard Library thread:

<!-- m -->http://www.autohotke... ... rd library<!-- m -->

The example that you sent to me earlier in this thread did not contain that period (.)
COM_Invoke(wmp, "currentPlaylist", "+" playlist)and adding that period did defeat operation. Perhaps it has a different significance.

(2) Could you embed the date in the header of the COM.ahk file of future versions of the COM Standard Library? That would facilitate checking that date later.

Progress to date in using playState

While I have gotten three examples (discussed below) of the use of playState to work with a hidden version of Windows Media Player 11, I have not yet figured out how to get the playState of the normal, visible version of Windows Media Player 11.

My hope was to read the playState of the normal, visible version of Windows Media Player 11 and to monitor it. With that capability, I could determine when playing actually begins, so that other actions could be triggered at the right moment. This is particularly helpful when Internet radio and Internet TV stations, with long buffering times, are being accessed.

I have also not yet found a way to feed the URL of a desired Internet radio station or Internet television station directly into the normal, visible version of the Windows Media Player. I can, of course, feed that URL in using ^u to activate the "Open URL" menu and then SendInput to send the individual keystrokes of the URL; but I was hoping for a more direct method.

Three successful examples using playState

The following three scripts provide successful examples of retrieving the playState of a hidden version of Windows Media Player 11, running under Windows XP. Perhaps these examples will prove helpful to other users.

(1) This first script gathers certain data about the CD-ROM drives and the CD-ROM in the first such drive, then evokes a hidden version of the Windows Media Player to play that CD-ROM, and then monitors the evolution of playState of that hidden version, once a second for five seconds. I have documented this script internally.

; Demonstration of several COM functions related
; to playing CD-ROMs in the Windows Media Player.
; Thanks to Lexikos and Sean for the methods.
; Thanks to Sean for the COM library.

MsgBox,
(
Insert CD-ROM into first CD-ROM drive. Then click on OK to cause a
hidden version of the Windows Media Player to play that CD-ROM.
Then wait 5 seconds for a MsgBox reporting the values of key parameters
and the time evolution of the "playState" of the hidden version of 
the Windows Media Player.
)

{
COM_Init()

; (1) and (2) thanks to Lexikos

; (1) Get the number of CD-ROM drives in the computer

    wmp := COM_CreateObject("WMPlayer.OCX")			; Reference:  Not sure of ultimate reference, but see Sean's COM functions at
								; http://www.autohotkey.com/forum/topic22923.html&highlight=com+functions
    cdroms := COM_Invoke(wmp, "cdromCollection")		; MSDN Reference:  http://msdn.microsoft.com/en-us/library/bb249303(VS.85).aspx
    number_of_CD_ROM_drives := COM_Invoke(cdroms, "count")	; MSDN reference:  http://msdn.microsoft.com/en-us/library/bb262138(VS.85).aspx

; (2) Get the number of tracks on the first CD-ROM drive

    which_CD_ROM = 0 ; the first (and only CD_ROM on this computer) is number 0
    cdrom := COM_Invoke(cdroms, "item", which_CD_ROM)		; MSDN reference: http://msdn.microsoft.com/en-us/library/bb262140(VS.85).aspx
    playlist := COM_Invoke(cdrom, "playlist")			; MSDN reference: http://msdn.microsoft.com/en-us/library/bb262145(VS.85).aspx
    tracks_on_CD := COM_Invoke(playlist, "count")		; MSDN reference: http://msdn.microsoft.com/en-us/library/bb249404(VS.85).aspx

; (3) thanks to Sean

; (3) Command a hiddeen version of the Windows Media Player to play
;     the CD-ROM in the CD-ROM drive.  Then capture the playState of that
;     CD-ROM drive immediately, and after each of the next five seconds,
;     to show how playState evolves.

    Result := COM_Invoke(wmp, "currentPlaylist", "+" playlist)	; playlist is a COM object, so the prefix + is required when used as a parameter.
								; MSDN reference:  http://msdn.microsoft.com/en-us/library/bb249313(VS.85).aspx#playercurrentplaylist
								; See also format for Sean's COM functions at:
								; http://www.autohotkey.com/forum/topic22923.html&highlight=com+functions 
    
    controls := COM_Invoke(wmp, "controls")			; MSDN reference: http://msdn.microsoft.com/en-us/library/bb249309(VS.85).aspx
    COM_Invoke(controls, "play")				; MSDN reference: http://msdn.microsoft.com/en-us/library/bb262211(VS.85).aspx 
    playState_0000 := COM_Invoke(wmp, "playState")		; MSDN reference: http://msdn.microsoft.com/en-us/library/bb249361(VS.85).aspx
    Sleep 1000
    playState_1000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_2000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_3000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_4000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_5000 := COM_Invoke(wmp, "playState")

MsgBox,
(
Windows Media Player:

(1) Get the number of CD-ROM drives in the computer

   wmp = %wmp%			number > 0 means working
   cdroms = %cdroms%		number > 0 means working
   number_of_CD_ROM_drives = %number_of_CD_ROM_drives%

(2) Get the number of tracks on the first CD-ROM drive

   cdrom = %cdrom% 		number > 0 means working
   playlist = %playlist%	number > 0 means working
   tracks_on_CD = %tracks_on_CD%
 
(3) Command a hidden version of the Windows Media Player to play
    the CD-ROM in the CD-ROM drive.  Then capture the playState of that
    CD-ROM drive immediately, and after each of the next five seconds,
    to show how playState evolves.

   Result = %Result% 		0 means working, nothing(null) means not working
   controls = %controls%	number > 0 means working

   playState_0000 = %playState_0000%	playState at time  0 seconds
   playState_1000 = %playState_1000%	playState at time  1 second
   playState_2000 = %playState_2000%	playState at time  2 seconds
   playState_3000 = %playState_3000%	playState at time  3 seconds
   playState_4000 = %playState_4000%	playState at time  4 seconds
   playState_5000 = %playState_5000%	playState at time  5 seconds

   Meaning of playState values:

	0 	Undefined 	
	1 	Stopped 	
	2 	Paused 	        
	3 	Playing 	
	4 	ScanForward 	
	5 	ScanReverse 	
	6 	Buffering 	
	7 	Waiting 	
	8 	MediaEnded 	
	9 	Transitioning 	
	10 	Ready
	11 	Reconnecting

    MSDN Reference:  http://msdn.microsoft.com/en-us/library/bb249361(VS.85).aspx
)

COM_Release(controls)
COM_Release(playlist)
COM_Release(cdroms)
COM_Release(cdrom)
COM_Release(wmp)

COM_Term()
}

Exit

(2) This second script uses a a different method, called WMPCD, to evoke a hidden version of Windows Media Player and to play a specific track on a CD-ROM in a specific CD-ROM drive. Then the script monitors the evolution of the playState of that hidden version, once a second for five seconds.

MsgBox,
(
Click on OK to command a hidden version of the
Windows Media Player to play track 2 on the first
CD-ROM drive in the computer.  Then wait five seconds
for a second MsgBox reporting on the evolution of the
playState during that period.
)

COM_Init()

    wmp := COM_CreateObject("WMPlayer.OCX")
    url := COM_Invoke(wmp, "Url","wmpcd://0/2" ) ; first drive has drive number 0. Track 2 is specified.
						 ; MSDN reference:  http://msdn.microsoft.com/en-us/library/bb249720(VS.85).aspx

    playState_0000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_1000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_2000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_3000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_4000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_5000 := COM_Invoke(wmp, "playState")


MsgBox,
(
   wmp = %wmp%				number > 0 means success
   url = %url%				

   playState_0000 = %playState_0000%	playState at time  0 seconds
   playState_1000 = %playState_1000%	playState at time  1 second
   playState_2000 = %playState_2000%	playState at time  2 seconds
   playState_3000 = %playState_3000%	playState at time  3 seconds
   playState_4000 = %playState_4000%	playState at time  4 seconds
   playState_5000 = %playState_5000%	playState at time  5 seconds

   Meaning of playState values:

	0 	Undefined 	
	1 	Stopped 	
	2 	Paused 	        
	3 	Playing 	
	4 	ScanForward 	
	5 	ScanReverse 	
	6 	Buffering 	
	7 	Waiting 	
	8 	MediaEnded 	
	9 	Transitioning 	
	10 	Ready
	11 	Reconnecting

   MSDN Reference:  http://msdn.microsoft.com/en-us/library/bb249361(VS.85).aspx
)


COM_Release(wmp)
COM_Term()

Exit

(3) This third script evokes a hidden version of the Windows Media Player, plays an Internet radio station, and then monitors the evolution of the playState of that hidden version, once a second for five seconds.

MsgBox,
(
Click on OK to command a hidden version of the Windows Media Player
to play an Internet radio station, WAMU, which is public radio from
American University in Washington, D.C.
Then wait five seconds for another message box, reporting
on the evolution of the "playState" over that five seconds.
)

COM_Init()

    wmp := COM_CreateObject("WMPlayer.OCX")
    url_to_play := "http://wamu.org/streams/live/1/win"
    url := COM_Invoke(wmp, "Url", url_to_play)               ; MSDN Reference:
; http://msdn.microsoft.com/en-us/library/bb249371(VS.85).aspx
 
    playState_0000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_1000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_2000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_3000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_4000 := COM_Invoke(wmp, "playState")
    Sleep 1000
    playState_5000 := COM_Invoke(wmp, "playState")

MsgBox,
(
   Windows Media Player:
  
   wmp = %wmp%				number > 0 means success
   url = %url%				0 is returned whether url_to_play is valid or not

   playState_0000 = %playState_0000%	playState at time  0 seconds
   playState_1000 = %playState_1000%	playState at time  1 second
   playState_2000 = %playState_2000%	playState at time  2 seconds
   playState_3000 = %playState_3000%	playState at time  3 seconds
   playState_4000 = %playState_4000%	playState at time  4 seconds
   playState_5000 = %playState_5000%	playState at time  5 seconds

   Meaning of playState values:

	0 	Undefined 	
	1 	Stopped 	
	2 	Paused 	        
	3 	Playing 	
	4 	ScanForward 	
	5 	ScanReverse 	
	6 	Buffering 	
	7 	Waiting 	
	8 	MediaEnded 	
	9 	Transitioning 	
	10 	Ready
	11 	Reconnecting

   MSDN Reference:  http://msdn.microsoft.com/en-us/library/bb249361(VS.85).aspx
)


COM_Release(wmp)
COM_Term()

Exit

I am very grateful to all who have helped me get this far, including Lexikos, Sean, Guest, and jaco0646.

Ron1

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
Re: (1):
the period is the technical operator for concatenate. It is somewhat optional, and AHK is fine without it.
var = This
msgbox % var . " is one way to do it"
msgbox % var " is another"


Ron1
  • Members
  • 42 posts
  • Last active: Apr 01 2014 06:58 PM
  • Joined: 20 Dec 2007
Thank you, engunneer. I will explore that concatenate operator.

Ron1