Playability of a file in windows media player

Discussion in 'Python' started by sri2097, May 3, 2006.

  1. sri2097

    sri2097 Guest

    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.
    sri2097, May 3, 2006
    #1
    1. Advertising

  2. sri2097

    Roger Upole Guest

    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" <> wrote in message news:...
    > 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.
    >
    Roger Upole, May 4, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?am9obmcxNDky?=

    Windows media player and asp.net

    =?Utf-8?B?am9obmcxNDky?=, Mar 1, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Alvin Bruney [MVP]
    Mar 4, 2004
  2. icho
    Replies:
    0
    Views:
    344
  3. cyshao
    Replies:
    1
    Views:
    3,303
    Steve C. Orr [MVP, MCSD]
    Apr 12, 2005
  4. ary
    Replies:
    1
    Views:
    1,068
    =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
    Nov 28, 2006
  5. Replies:
    1
    Views:
    960
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Jul 25, 2007
Loading...

Share This Page