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?