Windows Media Player 9 with win32com.client.Dispatch?

165 views
Skip to first unread message

Mickel Grönroos

unread,
Mar 23, 2004, 4:16:46 AM3/23/04
to pytho...@python.org
Hi!

Anybody tried using Windows Media Player 9 via win32com? (And more
precisely, combining that with a Tkinter application.)

I found a few earlier threads on Windows Media Player and Python, but it
seems nobody has really got it to work, e.g. the below does not work:

wmp = win32com.client.Dispatch("MediaPlayer.MediaPlayer.1")
wmp.FileName = "C:/Path/To/Some/Media/File.wav"
wmp.Play()

Is "MediaPlayer.MediaPlayer.1" really the progid to use for Windows Media
Player 9?

Is there some other way to use Windows Media Player from Python?
Embedding C++ in Python? Any code examples?

Thanks in advance.

/Mickel G.

--
Mickel Grönroos, application specialist, linguistics, Research support, CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi

Mickel Grönroos

unread,
Mar 23, 2004, 6:36:45 AM3/23/04
to pytho...@python.org
On Tue, 23 Mar 2004, Mickel Grönroos wrote:

> Anybody tried using Windows Media Player 9 via win32com?

> [...]


> Is "MediaPlayer.MediaPlayer.1" really the progid to use for Windows Media
> Player 9?

Hello again,

I got this far:

from win32com.client import Dispatch
wmp = Dispatch("WMPlayer.OCX")
wmp.openPlayer("C:\\Path\\To\\Media\\File")

This opens Windows Media Player 9 on my Win2000 box and starts playing the
media file. However, I would like to be able to control the media player
as well, and it is not fully self-evident how one does that.

I tried:

control = wmp.controls

This gives me a IWMPControls object that has methods for playing, pausing,
stopping etc. However, nothing happens when executing those methods, i.e.
the following simply returns None:

control.play()

Any ideas?

Dave Brueck

unread,
Mar 23, 2004, 3:44:56 PM3/23/04
to python-list
Mickel wrote:
> Anybody tried using Windows Media Player 9 via win32com? (And more
> precisely, combining that with a Tkinter application.)

I've used it before with wxPython. Below is a class I made for that purpose.
WMP was originally the module generated by makepy.py (I renamed it to
facilitate using it w/ py2exe):

# Created by makepy.py version 0.4.0
# By python version 2.2.1 (#34, Apr 15 2002, 09:51:39) [MSC 32 bit (Intel)]
# From type library '{22D6F304-B0F6-11D0-94AB-0080C74C7E95}'
# On Wed Jan 22 12:34:30 2003

Here's the class, I'm not sure how helpful it is since it relies on some
wxPython-specific stuff, but...

class wxWMPlayer(wxPanel):
def __init__(self, parent, stateChangeCB):
self.parent = parent
self.stateChangeCB = stateChangeCB
wxPanel.__init__(self, parent, -1,
style=wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE)
theClass = MakeActiveXClass(WMP.MediaPlayer, eventObj = self)
self.player = theClass(self, -1)
self.player.AutoSize = 1
self.player.ShowControls = 0
self.player.DisplaySize = WMP.constants.mpFitToSize
self.isPlaying = 0

self.sizer = sizer = wxBoxSizer(wxVERTICAL)
sizer.Add(self.player, 1, wxEXPAND)
self.SetSizer(sizer)
self.SetAutoLayout(1)

def SetFile(self, filename):
self.parent.SetCursor(wxHOURGLASS_CURSOR)
self.player.AutoStart = 0
self.isPlaying = 0
self.player.FileName = filename
self.parent.SetCursor(wxNullCursor)

def Play(self): self.player.Play()
def Pause(self): self.player.Pause()
def IsPlaying(self): return self.isPlaying

def GetDuration(self): return self.player.Duration

def OnPlayStateChange(self, oldState, newState):
self.isPlaying = (newState == WMP.constants.mpPlaying)
self.stateChangeCB(self.isPlaying)

HTH,
Dave


Mickel Grönroos

unread,
Mar 24, 2004, 2:47:25 AM3/24/04
to Dave Brueck, python-list
> Here's the class, I'm not sure how helpful it is since it relies on some
> wxPython-specific stuff, but...

Thanks Dave! wxPython is surely a problem here; I'm stuck using Tkinter.
Do you know how well those two GUI toolkits interoperate?

Cheers,

Mickel

Reply all
Reply to author
Forward
0 new messages