Windows Media Player COM自动化可从VBS运行,但不能从Python运行 [英] Windows Media Player COM Automation works from VBS but not from Python

查看:95
本文介绍了Windows Media Player COM自动化可从VBS运行,但不能从Python运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Windows Media Player通过COM播放音频文件.以下代码在VBS中可以正常工作:

I try to use Windows Media Player to play audio files via COM. The following code works fine in VBS:

Set wmp = CreateObject("WMPlayer.OCX")
wmp.settings.autoStart = True
wmp.settings.volume = 50
wmp.URL = "C:\Windows\Media\tada.wav"
while wmp.Playstate <> 1
WSH.Sleep 100
wend

不幸的是,相同的代码在Python中无法播放任何声音:

Unfortunately, the identical code doesn't play any sound in Python:

import win32com.client
import time

wmp = win32com.client.dynamic.Dispatch("WMPlayer.OCX")
wmp.settings.autoStart = True
wmp.settings.volume = 50
wmp.URL = r"C:\Windows\Media\tada.wav"
while wmp.Playstate != 1:
    time.sleep(0.1)

COM交互似乎很困难,因为我可以创建新的媒体对象并查询有关它们的信息.只是听不到声音.

COM interaction seems to work tough, as I can create new media objects and query info about them. It's just that no sound is ever audible.

>>> media = wmp.newMedia(r"C:\Windows\Media\tada.wav")
>>> media.durationString
'00:01'
>>> wmp.currentMedia = media
>>> wmp.play()                 # No sound audible.
>>> wmp.PlayState
9                              # wmppsTransitioning

始终报告

PlayState

PlayState is always reported to be wmppsTransitioning, no matter what I do.

在最后两个PyWin32版本(218和219)的Python2.7、3.2和3.3中出现了问题.操作系统是Windows 7 x64,Python解释器全部编译为32位. WMPlayer.OCX 可以成功加载并且COM可以正常工作,因此我认为这不是32位/64位DLL问题.

The problem appears with Python2.7, 3.2 and 3.3 with the last two PyWin32 versions (218 and 219). OS is Windows 7 x64, the Python interpreters are all compiled for 32 bits. WMPlayer.OCX can be loaded successfully and COM works, so I don't think this is a 32bit/64bit DLL issue.

有人知道为什么它可以与VBS一起使用而不与Python一起使用吗?我该如何进一步调试呢?

Any idea why it works with VBS and not with Python? How could I further debug this?

推荐答案

似乎问题在于 time.sleep 不会弹出窗口消息.使用其他一些超时功能来弹出窗口消息.

It seems the problem is that time.sleep doesn't pump window messages. Use some other timeout function that pumps window messages.

原因是Windows Media Player是STA组件,很可能是因为它最常用作图形组件.其某些内部功能取决于常规消息泵送,最有可能是发送窗口消息进行通信的高精度多媒体计时器线程,或者可能取决于实际的 WM_TIMER 消息.

The reason is that Windows Media Player is an STA components, most probably because it's most commonly used as a graphical component. Some of its internals depend on regular message pumping, most probably a high-precision multimedia timer thread that sends window messages for communication, or it might depend on actual WM_TIMER messages.

这篇关于Windows Media Player COM自动化可从VBS运行,但不能从Python运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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