Windows Media Player 9 with win32com.client.Dispatch?

Dave Brueck dave at pythonapocrypha.com
Tue Mar 23 15:44:56 EST 2004


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





More information about the Python-list mailing list