import pygst,sys,time,gobject pygst.require("0.10") import gst import gst from duration import Duration class Song(): def __init__(self,file): # initialize player pipeline self.player = gst.Pipeline("player") bin = gst.element_factory_make("playbin",None) bin.set_property("uri","file://"+file) self.player.add(bin) # initialize bus bus = self.player.get_bus() bus.add_signal_watch() bus.connect("message",self.onMessage) # Pause song self.pause() # Initialize stuff for finding duration self.time_format = gst.Format(gst.FORMAT_TIME) self.length = None # Start mainloop gobject.threads_init() # Called on bus messages def onMessage(self,bus,message): t = message.type if t == gst.MESSAGE_EOS: print "End of stream" sys.exit(0) elif t == gst.MESSAGE_ERROR: print "Error" sys.exit(0) elif t == gst.MESSAGE_TAG: print "Tags:" # Probably should convert this to some other data structure # For easier use later taglist = message.parse_tag() for key in taglist.keys(): print key,taglist[key] return # Change state to "playing" def play(self): self.player.set_state(gst.STATE_PLAYING) # Start main loop and find duration # If this hasn't been done yet if self.length == None: time.sleep(0.2) self.duration() # Change state to "paused" def pause(self): self.player.set_state(gst.STATE_PAUSED) # Find the duration of the pipeline def duration(self): self.length = Duration() length = self.player.query_duration(self.time_format,None)[0] self.length.setTime(length) self.length.disp() # Print out current running time def curTime(self): length = self.player.query_position(self.time_format,None)[0] return Duration().setTime(length)