3

I am trying to integrate the current media name/artist in my python app and I can't seem to get it to work.

I did the same thing with iTunes with almost no issues whatsoever but for some reasons I can't manage to get the correct instance of windows media player

wmp = win32com.client.gencache.EnsureDispatch("WMPlayer.OCX")

gets me a valid wmp instance but when I try to get the current media using

wmp.currentMedia

It returns nothing whatsoever and getting the volume like this:

wmp.settings.volume

returns a different volume than my actual opened windows media player volume.

Is there something I'm not aware of? I tried using WMPlayer.OCX.7 as well, but that doesn't work.

peterh
  • 11,875
  • 18
  • 85
  • 108
  • I'm pretty sure that what your code currently does is create a separate instance of the media player. – Eric Brown Mar 17 '14 at 00:56
  • Only possible if you run python 2.x, as you need to implement a custom interface using `comtypes` Infos here: http://stackoverflow.com/questions/19452001/need-a-way-to-retrieve-the-current-playing-song-from-zune-and-windows-media-play and here http://stackoverflow.com/questions/19613750/heavily-confused-by-win32api-com-and-an-answer-from-so – manuell Mar 17 '14 at 09:44

2 Answers2

1

Windows Media Player supports several runnable instances. Creating the OCX in the way you're doing creates a separate instance of the player, which, of course, doesn't have any media loaded.

The usual way to find the "master" instance would be to look in the Running Object Table, but Windows Media Player doesn't do that. Instead, you need to use "remote" mode.

This isn't terribly well documented, but there's a VB.Net sample here, translated from a C# sample here. There are C++ samples in the Windows SDK, in the \Multimedia\WMP_11\cpp\RemoteSkin directory.

None of this, unfortunately, is in Python. But hopefully the existing samples will help you out.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
0

Maybe try and use something like GetObject(None, "WMPlayer.OCX")) insted of EnsureDispatch? I think that EnsureDispatch makes a separate .exe instance typically. Using GetObject should 'latch' onto the current instance and make it available as an object.

Radical Edward
  • 5,234
  • 5
  • 21
  • 33
  • This is a very good tip, unfortunately this lead to an `Operation not available` error (-2147221021). – Epoc Mar 30 '17 at 12:21