Playability of a file in windows media player

Roger Upole rupole at hotmail.com
Thu May 4 01:56:06 EDT 2006


The below code will catch the OnError event that's
triggered when you specify a bad URL.

import win32com.client
class wmpevents:
    def OnOpenStateChange(self, NewState):
        """Sent when the control changes OpenState"""
        print 'OnOpenStateChange', NewState
        if NewState==win32com.client.constants.wmposMediaOpen:
            print "Media successfully opened"
    def OnError(self):
        """Sent when the control has an error condition"""
        print 'OnError'
        print self.Error.Item(0).errorCode, self.Error.Item(0).errorDescription
        self.Error.clearErrorQueue()

win32com.client.gencache.EnsureDispatch('WMPlayer.OCX',0)
w=win32com.client.DispatchWithEvents('WMPlayer.OCX', wmpevents)
w.URL='some bad URL'

        Roger

"sri2097" <srikar2097 at gmail.com> wrote in message news:1146660038.890159.167130 at j73g2000cwa.googlegroups.com...
> Basically,
> I want to check if a URL is playable or not (without actually playing
> it).
> i.e. given a URL I want to write an automation script to figure it out
> for me
> if it's playable or not. If you give a bad/invalid URL for windows
> media
> player to play a pop-up window shows us that it cannot be played. So
> I would want to catch that event.
>
> I have found 2 ways to do this -
>
> 1)
>
> import win32com.client, win32api, sre, time
>
> data = file("webclips.txt")
> web_clips = data.readlines ()
>
> shell = win32com.client.Dispatch("WScript.Shell")
> shell.Run("wmplayer.exe")
> shell.AppActivate("Windows Media Player")
> win32api.Sleep(100)
>
> print "Total :", len(web_clips)
>
> for webclip in web_clips:
>    shell.SendKeys("^u", 0)
>    shell.AppActivate("Open URL")
>
>    shell.SendKeys("{DEL}")
>    shell.SendKeys(webclip)
>    shell.SendKeys("~")
>    time.sleep(25)
>
>    if shell.AppActivate("Windows Media Player"):
>        webclip = webclip
>        not_there.append(webclip)
>        shell.SendKeys("~")
>
> print len(not_there)
> print "Not here: ", not_there
> ~
>
> In this way I manually fire Windows media player and do the checking.
> But It's a brute force way of solving the problem (since for every URL
> I keep a
> time-out of 25 seconds). As a result it takes a lot of time.I had to
> look for a
> better solution. My second solution uses Windows much hyped ActiveX
> controls.
>
> 2)
>
> from win32com.client import Dispatch,DispatchWithEvents
>
> class WMPEvents:
>    def OnVisible(self,evt):
>        print "OnVisible changed:",evt
>    def OnError(self,evt=None):
>        print "OnError",evt
>    def OnMediaError(self,evt=None):
>        print "OnMediaError",evt
>    def OnDisconnect(self,evt):
>        print "OnDisconnect",evt
>    def OnStatusChange(self):
>        print "OnStatusChange"
>    def OnDisconnect(self,evt):
>        print "Disconnect",evt
>    def OnBuffering(self,evt):
>        print "OnBuffering changed:",evt
>    def OnOpenStateChange(self,evt=None):
>        print "OnOpenStateChange" ,evt
>
> mp = DispatchWithEvents("WMPlayer.OCX", WMPEvents)
> mp.settings.autoStart = True
> webclip_playlist = mp.newPlaylist('Web_Clips', "")
>
> raw_web_clips = []
> data = file("webclips.txt")
> web_clips = data.readlines()
>
> for url in web_clips:
>        tune = mp.newMedia(url)
>        mp.currentPlaylist.appendItem(tune)
>        mp.controls.playItem (tune)
>        mp.controls.play()
>        mp.controls.stop()
>
> raw_input("Press enter to stop playing")
> mp.controls.stop()
> mp.close()
>
> This solution is much faster. But still I'm not able to solve it.
> Initially I had
> planned to use the "OnOpenStateChange" event to detect if a given URL
> is in
> a state just about to be opened. That's good enough for me to declare
> that a
> URL can be played. But as written in MSDN, they suggest not to rely on
> state
> changes as a definitive way to find details. So, I had to find an
> alternative.
> There is an event called "OnBuffering", i.e. when a link is found and
> is just
> about to be buffered this event (Boolean value) is triggered. But it
> never seem
> to happen.
>
> I would be truly grateful if you can help in any way.
> 





More information about the Python-list mailing list