Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Playability of a file in windows media player

Reply
Thread Tools

Playability of a file in windows media player

 
 
sri2097
Guest
Posts: n/a
 
      05-03-2006
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.

 
Reply With Quote
 
 
 
 
Roger Upole
Guest
Posts: n/a
 
      05-04-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" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) oups.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.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows 7, Media Player - Settings for Media Serving NOT being savedfor Home Group Tim Wells Windows 64bit 0 10-27-2009 01:26 PM
Re: DVD playability Stephen Worthington NZ Computing 6 09-27-2009 11:11 PM
Re: DVD playability Lawrence D'Oliveiro NZ Computing 0 09-26-2009 12:17 AM
Windows Media Player - Download Media Info From Internet is Greyed Out Alan NZ Computing 3 02-27-2006 10:38 PM
mp3 player windows media player Bigfred Computer Support 1 10-28-2003 09:46 PM



Advertisments