how to know if windows media player is playing or not? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

how to know if windows media player is playing or not?

23 Dec 2017, 08:13

I tried several codes (such as these https://autohotkey.com/board/topic/30791-can-ahk-get-play-state-of-windows-media-player/://autohotkey.com/board/topic/30791-can-ahk-get-play-state-of-windows-media-player/?&st=0 ) but they don't seem to work anymore (maybe they are not compatible anymore with the newer version of ahk 1.1.26.01 or with windows 10).


Anyone knows how to simply know if wmp is playing or not?


PS: the code linked above give this error:
Error: Call to nonexistent function.
Specifically: COM_Init()
Rindis
Posts: 181
Joined: 23 Dec 2013, 13:58
Location: Norway

Re: how to know if windows media player is playing or not?

23 Dec 2017, 08:46

Have you downloaded https://autohotkey.com/board/topic/2114 ... d-library/ ? (and put it in the same folder as your script)
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

23 Dec 2017, 10:31

Thanks for your help!
I guess I need to add

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include C:\Users\Me\Desktop\my_folder\COM.ahk
at the beginning of the script (which is on the same folder than the github file)

When I run it I get the error below (when I run the COM.ahk it sends the same error):

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Guest

Re: how to know if windows media player is playing or not?

23 Dec 2017, 12:27

It's getting close, the com.ahk is working (but the one from Sean here https://github.com/ttnnkkrr/COM.AHK/releases/tag/1.0, the one on github you gave me doesn't work).


But I get an new error.

I tried this code (the smallest):

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

#Include C:\Users\Me\Desktop\Ahk\com.ahk
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")


But I get this error

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

---------------------------
COM Error Notification
---------------------------
Function Name: "controls"
ERROR: No COM Dispatch Object!
()
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

23 Dec 2017, 12:51

(Sorry, for some reason it posted as guest)


I tried that:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

#Include com.ahk
com_Init()
playState := COM_CreateObject(wmp, "playState")
MsgBox %playState%


and that

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

#Include com.ahk
com_Init()
playState := COM_Invoke(wmp, "playState")
MsgBox %playState%





it sends nothing (the play is not detected)
I find this microsoft page, but I don't know how to use that information in the syntax
https://msdn.microsoft.com/en-us/librar ... 2147217396
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: how to know if windows media player is playing or not?  Topic is solved

24 Dec 2017, 10:40

This works here (Windows 10 1703 / WMP 12 / AutoHotkey x64 1.1.26.01), but given the complexity of performing such a simple task and my inexperience, I can't say it'll work for you.

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4811
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to know if windows media player is playing or not?

24 Dec 2017, 13:20

- Well, I finally ran Windows Media Player for the first time on this PC, which I've had for ~3 years (C:\Users, file properties), and dealt with the dialogs. (I use Media Player Classic.)
- I managed to get a script working with Acc, although it may need to be modified based on what mode is on (Library/Skin/Now Playing).
- Also, there may be multiple windows called 'Windows Media Player ahk_class WMPlayerApp' which adds to the confusion. That is, even when you only have one instance open.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

26 Dec 2017, 08:27

Thank you so much for your help!

@Qwerty: my mind exploded while trying to follow your code...
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: how to know if windows media player is playing or not?

26 Dec 2017, 09:07

No problem, partof :-)

partof wrote:@Qwerty: my mind exploded while trying to follow your code...


Sorry, while I'm usually rather lazy when it comes to commenting, that script took me a while to write so I wasn't particularly enthused about the idea of going back to it in this case...

The stuff after the ; --- is my crude implementation of a COM object that has only one purpose: to return the string "Remote" when the Windows Media Player COM object asks, which tells the WMPlayer.OCX object to abandon efforts on hosting its own instance of WMP and to instead focus its efforts on controlling an instance of WMP started by you. Sadly, all that boilerplate is required just to get to that point. I should point out that something similar (but not for WMP) was done in far, far fewer lines here. Sadly, I did not see that before I had already written most of, well, what you see now.

Code: [Select all] [Download] GeSHi © Codebox Plus

rms := IWMPRemoteMediaServices_CreateInstance()
ocs := ComObjQuery(rms, "{00000118-0000-0000-C000-000000000046}")


^ is the COM object after the ; --- being created and obtaining a pointer to the interface implementing IOleClientSite

Code: [Select all] [Download] GeSHi © Codebox Plus

ole := ComObjQuery(wmp, "{00000112-0000-0000-C000-000000000046}")
DllCall(NumGet(NumGet(ole+0)+3*A_PtrSize), "Ptr", ole, "Ptr", ocs)


this asks for the IOleObject interface from the WMPlayer.OCX object and the DllCall line then plugs our IOleClientSite interface into WMPlayer.OCX's IOleObject - this starts the process of the OCX object asking our COM object what WMP it should control - eventually, our object responds with "Remote"

Code: [Select all] [Download] GeSHi © Codebox Plus

states := {0: "Undefined", 1: "Stopped", 2: "Paused", 3: "Playing"}
state := states[wmp.playState]


Hopefully, all went well and it's connected to your running WMP. So now we can ask for the current state. (You could also ask for the current filename and Title tag at this point: MsgBox % wmp.currentMedia.name and MsgBox % wmp.currentMedia.sourceURL respectively)

Code: [Select all] [Download] GeSHi © Codebox Plus

DllCall(NumGet(NumGet(ole+0)+3*A_PtrSize), "Ptr", ole, "Ptr", 0)
for _, obj in [ole, ocs, rms]
ObjRelease(obj)
wmp := ""


This starts the cleanup and disconnection from the WMPlayer interface. The first line tells the WMPlayer object to stop using our object to determine what WMP it should control and the next lines release the references we hold to the WMPlayer.OCX and our long object at the bottom of the script - hopefully, the RAM they're using gets released

MsgBox %state%

Finally, get the state. The reason why I did this last is because MsgBox will block your script, and if the WMPlayer.OCX object is still connected to a remote WMP, it will restart WMP in the background if you happen to close WMP while the script is showing its state dialog
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

26 Dec 2017, 09:56

@jeeswg I also tried your code (I followed your instructions here https://autohotkey.com/boards/viewtopic.php?f=6&t=26201 ) .
It doesn't work (it always believe it's playing)

I'm trying to understand how it works (I'm discovering the Acc tool which opens many possibilities for other code!)
"worked when Library mode was on" : I guess the library is on if the Acc is working.

Here is what the Acc gives (button + main screen)
Image
Image

I tried (I just changed the "windows Media Player" instead) :

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

ControlGet, hCtl, Hwnd,, Windows Media Player, A
oAcc := Acc_Get("Object", "4.1.16.2.14.3", 0, "ahk_id " hCtl)
vState := oAcc.accName(0)
oAcc := ""
MsgBox, % "is playing: " (vState = "Play" ? "n" : "y")
return


But it seems that I'm missing some information:
Line 1: What means the "A"?
Line 2: Where did you get that "4.1.16.2.14.3" ? What the 0 stand for? Did you get those from Acc? And is oAcc suppose to select the whole wmp windows or just the get info about the play/pause button?
Line 3: what is it doing, what's the 0 for ?
Line 4: by curiosity: why do you empty this var ?
User avatar
jeeswg
Posts: 4811
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to know if windows media player is playing or not?

26 Dec 2017, 10:08

- On Windows 7 (and possibly other OSes), Windows Media Player has 3 modes: Library/Skin/Now Playing. The mode determines whether the script works. I realise now that I should edit the script, if the text found is neither 'Play' nor 'Pause' it should report unknown. I might experiment further to get it working in all 3 modes. I found some quirks of Windows Media Player, regarding multiple windows with the same name/class that made it harder to investigate.
- 'A' means active window.
- I obtained '4.1.16.2.14.3' via a script, the link is at the top of the script.
- The '0' is to specify not to use a child item. Actually I'm not entirely sure when to use accName / accName() / accName(0). IIRC generally the latter is safest.
- I obtained the information using the Acc library and a custom function that I wrote. You can sometimes use AccViewer to retrieve such information, although Windows Media Player wasn't playing nicely with AccViewer, it wasn't showing the treeview hierarchy.
- It's supposed to latch onto a specific GUI element and retrieve its text.
- vState := oAcc.accName(0) oAcc is an object representing the Play/Pause button, accName gets its text, I mentioned about the 0 above.
- I empty the variable afterwards, because like with most things in AHK/programming generally, when you latch onto something, but no longer need it, you should release it, otherwise you can get things like memory leaks.
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

26 Dec 2017, 10:11

@Qwerty Thanks you so much for your explanation! It's impressive (I wish I could download your brain). I'm deeply touched by all the time you have invested in this code (I find it as impressing as your code!).
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

26 Dec 2017, 10:38

@jeeswg : thanks a lot for your explanations I'm excited to be able to use it elsewhere! But, I still struggle with 2 points:

Question 1
I obtained '4.1.16.2.14.3' via a script, the link is at the top of the script

then you wrote (also about it?):
I obtained the information using the Acc library and a custom function that I wrote. You can sometimes use AccViewer to retrieve such information, although Windows Media Player wasn't playing nicely with AccViewer, it wasn't showing the treeview hierarchy.


About the first quote: I guess you are talking about information given by the AccViewer.ahk right? If so, in which field should I retrieve that information? (Cf. my print screen above). But you after wrote that you finally used a custom function to get that number. Is there a simple way to get that number?

And what is it called by the way? [update, you just replied: it's the "acc path"]

Question 2

it may need to be modified based on what mode is on (Library/Skin/Now Playing)

which specific part of your code should be modified to set the right mode? (I don't see any of those word in your code)
Last edited by partof on 26 Dec 2017, 11:13, edited 3 times in total.
User avatar
jeeswg
Posts: 4811
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to know if windows media player is playing or not?

26 Dec 2017, 10:52

- I got the path via this function:
Acc: get text from all window/control elements - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=40615
- Usually you can get the path via AccViewer, but it wasn't working with Windows Media Player.
- Re. Library mode. What I meant was, I'd only got the script to work, if Windows Media Player was in Library mode. So you'd have to set Windows Media Player manually to Library mode to get it working. Although, I suppose, setting the mode could also be automated. I will check over the script.
partof
Posts: 56
Joined: 16 Jan 2016, 08:38

Re: how to know if windows media player is playing or not?

26 Dec 2017, 11:57

I still did not manage to make it work (using jeeswg code) but I'm sharing the progress I made to understand his code:


update: the code below actually work (but I don't know why: cf the acc path...):
+ when WMP is set on "library" mode,
+ and only when I press "q" on the WMP (pressing "q" above another app doesn't work. It always set playing to "yes")

(The comments bellow explain some part of the code, but also ask for clarification for the others)

Code: [Select all] [Expand] [Download] (Untitled.ahk)GeSHi © Codebox Plus

User avatar
jeeswg
Posts: 4811
Joined: 19 Dec 2016, 01:58
Location: UK

Re: how to know if windows media player is playing or not?

01 Jan 2018, 12:40

- I managed to get a script to get the playing/paused state from Windows Media Player for all 3 states: Library/Skin/Now Playing. Tested on Windows 7. It needs the Acc library.
- The problem is that, usually with Acc, what you want is information from the main window, but for Windows Media Player, in two of the modes, you have to get information from a second window that isn't the main window.
- It would be interesting to know if there is a direct way to get from the hWnd of the main window, to the hWnd of the secondary window.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


- I used this code to get the hWnd for the window under the cursor (instead of the active window), and then retrieve all of the text for that window via Acc.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Ask For Help”

Who is online

Users browsing this forum: comvox, DarkACB, Google [Bot], TAC109 and 52 guests