Reputation: 7227
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
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 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
is always reported to be wmppsTransitioning
, no matter what I do.
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.
Any idea why it works with VBS and not with Python? How could I further debug this?
Upvotes: 3
Views: 1595
Reputation: 8135
It seems the problem is that time.sleep
doesn't pump window messages. Use some other timeout function that pumps window messages.
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.
Upvotes: 4