需要一种使用Python从Zune和Windows Media Player检索当前播放歌曲的方法 [英] Need a way to retrieve the current playing song from Zune and Windows Media Player with Python

查看:103
本文介绍了需要一种使用Python从Zune和Windows Media Player检索当前播放歌曲的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序从众多音乐播放器中检索当前正在播放的歌曲.但是,在实施Zune和Windows Media Player时遇到了很大的麻烦.

我在这个问题上做了很多研究,不幸的是,这只会使我越来越困惑.

通常我会为其他应用程序做什么:

  1. 每4秒对所有打开的窗口进行迭代
  2. 获取所有窗口的标题
  3. 检查模式的标题(例如," - Spotify ")
  4. 如果有,请调整输出标题.

WMP标题中没有当前正在播放的歌曲.

Zune可以,但标题,专辑和艺术家之间每隔几秒钟旋转一次.即使有可能,使用我当前的方法进行跟踪也非常不可靠.

Windows Media Player

我还尝试过将COM组件用于Windows Media Player.

import win32com.client
wmp = win32com.client.gencache.EnsureDispatch('WMPlayer.OCX')

# some function I don't have here, it retrieves the current playing song
# and other data

最大的问题是它需要您以编程方式启动WMP,这对用户非常不友好

那么,我发现了什么? 此SO帖子重定向到WMP.dll.但是,据我所读,它与COM存在相同的问题,您必须以编程方式启动它.如果没有,我真的很想知道如何在python中使用该dll的一些指导.

还有另一种不太麻烦的解决方案,那就是为WMP编写一个插件,让我的用户下载该插件并从该插件中检索数据.我宁愿不去那里,因为我没有使用任何C语言的经验,也不希望为此而研究插件文档.

Zune

一种方法是循环浏览三个标题状态,确定当前处于哪个状态并找到其他两个状态.

IE: 前5秒的标题是:Super_song 接下来的5秒标题是:作者Power_artist 接下来的5秒标题是:Good_album(日期)

因此,我可以通过为日期创建一个正则表达式来确定专辑标题的时间(该日期始终存在),然后等待几秒钟来查找标题和歌手.

这显然不是一个很好的解决方案,因为它会花费一些时间,而且也不是很可靠(例如,如果歌曲名称包含日期,该怎么办)

下一个问题是,它也不是一致的,有时标题会停留在Zune几分钟.不知道为什么.

所以,继续下一个方法.

有一个名为 ZuneNowPlaying 的应用程序.这种以某种方式"从Zune获取当前播放的歌曲并将其放入注册表中,但是这不适用于我的草率标题方法,因为它会在歌曲更改时立即更改注册表.马上.

这是我在程序的正常工作版本中使用的解决方案,但是许多用户报告说它根本不起作用,什么也没发生.而且我检查了程序,但它并不能始终可靠地更改注册表.我不知道为什么,我不知道如何解决.因此,此解决方案也是-scrapped-.

事实是它使用名称"MsnMsgrUIManager"#000000> 使zune软件向其发送有关哪首歌曲是 玩吗如果没有这种方法,是否有办法获取此信息 骇客?

可以在"Zune Now Play"应用程序的讨论中找到.不幸的是,该源不可用,至少我找不到.有人对此有更多了解吗?

我听说的第三种方法又是一个dll.它被称为ZuneShell.dll.我不记得在哪里读到它,也无法通过Google找到它,因为所有结果都是"ZuneShell.dll是病毒吗?".

再一次,我遇到了一个问题,我什至不知道如何处理这个 IF ,即使是我一直在寻找的文档,./p>

可能要研究的替代方向

在浏览此主题时,我看到有人谈论直接从GUI检索数据.我不确定我对它的记忆是否合法,可能甚至正确,但是如果可以的话,有人可以将我重定向到更多吗?

真的没什么

解决方案

我有使用C ++的有效代码来打印当前在WMP中播放的媒体的名称.这是一个简单的控制台应用程序(78行代码).

步骤:

1)实现了一个基本的COM对象,该对象实现了IUnknown,IOleClientSite,IServiceProvider和IWMPRemoteMediaServices.使用ATL模板CComObjectRootEx,这很简单(有点,您的里程可能会有所不同).唯一需要(简单)代码的方法是IServiceProvider :: QueryService和IWMPRemoteMediaServices :: GetServiceType.所有其他方法都可能返回E_NOTIMPL

2)(在我的情况下,通过CoCreateInstance实例化)"WMPlayer.OCX" COM对象

3)通过QueryInterface从对象检索IOleObject接口指针

4)从1)中看到的类实例化一个对象(我使用CComObject<> :: CreateInstance模板)

5)在3)的界面中使用SetClientSite方法,将指针传递给OleClientSite实现.

6):在SetClientSite调用期间,WMP将回调您:首先请求IServiceProvider接口指针,第二次调用QueryService方法,再请求IWMPRemoteMediaServices接口指针.返回您的IWMPRemoteMediaServices的实现,第三,将通过GetServiceType再次调用您.然后,您必须返回"Remote".现在,您已连接到WMP运行实例

7)在COM对象中查询IWMPMedia接口指针

8)如果7)没有提供NULL,请读取IWMPMedia :: name属性.

9)完成

以上所有内容均在VS2010/Windows 7和WMP运行下进行了测试(如果没有Media Player进程在运行,则什么也不做).

我不知道您是否可以/想要在Python中实现COM接口和对象.如果您对我的C ++代码感兴趣,请告诉我.您可以在C ++ DLL中使用该代码,然后从python调用它.

An Application of mine retrieves the current playing song from a multitude of music players. However, I'm having great trouble implementing Zune and Windows Media Player.

I've done a lot of googling on the subject, unfortunately it's only confusing me more and more.

What I would normally do for my other applications:

  1. Iterate over all open windows every 4 seconds
  2. Get the title of all windows
  3. Check title for a pattern (Ie, " - Spotify ")
  4. If it's there, adjust the title for output.

WMP Does not have the current playing song in the title.

Zune does, but it's rotating every few seconds between title, album and artist. Which is heavily unreliable to track with my current method, albeit possible.

Windows Media Player

I've also tried using the COM component for windows media player.

import win32com.client
wmp = win32com.client.gencache.EnsureDispatch('WMPlayer.OCX')

# some function I don't have here, it retrieves the current playing song
# and other data

The big problem with that it requires you to start WMP programmatically, which would be extremely user unfriendly

So, what have I found? This SO post redirects to WMP.dll. But as far as I've read, it has the same problem as the COM, you have to start it programmatically. If not, I would really like some directions on how to work with that dll in python.

There would be another a little less hacky solution, which is to write a plugin for WMP, let my users download that plugin and retrieve the data from that plugin. I'd rather not go there, since I have no experience with any of the C languages, nor do I feel like digging into plugin documentations for this.

Zune

A method would be to cycle through the three title states, determine which state it's currently at and find the position of the other two.

IE: First 5 seconds the title is: Super_song Next 5 seconds the title is: By Power_artist Next 5 seconds the title is: Good_album (date)

So I could determine when the album title is by making a regex for the date (which is always there) and then find the title and artist by waiting a few seconds.

This is obviously not a great solution, since it'll take a while and it's not very reliable either, (what if the song name contains a date for example)

The next problem is that it's not consistent either, sometimes the title just stays Zune for minutes long. No idea why.

So, move on to the next method.

There's this application called ZuneNowPlaying. This "somehow" gets the current playing song from Zune and puts it in the registry, this thing does not work with my sloppy title method, since it changes the registry the instant the song changes. Immediately.

This is the solution I had used in the working version of my program, but many users reported that it simply didn't work, nothing happened. And I checked the program and it doesn't reliably change the registry all the time. I don't know why, I don't know how to fix it. Therefor, this solution is also -scrapped-.

Is the fact that it is using the name "MsnMsgrUIManager"#000000"> causing the zune software to send it information about which song is playing? Is there a way to get this information without this kind of hack?

That is found in the discussion of the Zune Now Playing application. The source is not available unfortunately, at least I can't find it. Anyone got more on this?

Third method I had heard of was once again, a dll. ZuneShell.dll it's called. I don't remember where I read about it, nor can I find it via google, since all results are "Is ZuneShell.dll a virus?".

Once again, I run into the problem that I wouldn't know how to work with this even IF I had documentation on it, heck, if it's even what I have been looking for.

Alternate directions to maybe look into

While browsing about this subject, I've seen people talking about retrieving data directly from GUI's. I'm not sure how legit, possible or even how correct my memory of it is, but if it's possible could someone redirect me to more on this?

Anything else, really.

解决方案

I have working code in C++ to print the name of media currently playing in WMP. It's a simple console application (78 lines of code).

Steps:

1) implements a basic COM object implementing IUnknown, IOleClientSite, IServiceProvider and IWMPRemoteMediaServices. This is straightforward (sort of, your mileage may vary) using the ATL template CComObjectRootEx. The only methods needing (simple) code are IServiceProvider::QueryService and IWMPRemoteMediaServices::GetServiceType. All other methods may return E_NOTIMPL

2) Instantiate the "WMPlayer.OCX" COM object (in my case, via CoCreateInstance)

3) Retrieve from the object an IOleObject interface pointer via QueryInterface

4) Instanciate an object from the class seen in 1) (I use the CComObject<>::CreateInstance template)

5) Use the SetClientSite method from the interface you got at 3), passing a pointer to your OleClientSite implementation.

6) During the SetClientSite call, WMP will callback you: fisrt asking for an IServiceProvider interface pointer, second calling the QueryService method, asking for an IWMPRemoteMediaServices interface pointer. Return your implementation of IWMPRemoteMediaServices and, third, you will be called again via GetServiceType. You must then return "Remote". You are now connected to the WMP running instance

7) Query the COM object for an IWMPMedia interface pointer

8) If 7) didn't gave NULL, read the the IWMPMedia::name property.

9) DONE

All the above was tested with VS2010 / Windows Seven, and with WMP running (if there is no Media Player process running, just do nothing).

I don't know if yoy can/want to implement COM interface and object in Python. If you are interested by my C++ code, let me know. You could use that code in a C++ DLL, and then call it from python.

这篇关于需要一种使用Python从Zune和Windows Media Player检索当前播放歌曲的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆