import gst class Song(): #def __init__(self,info,exitFunc,prnt): def __init__(self,info,next): self.next = next self.info = info self.position = 0 self.player = gst.Pipeline("player") self.bin = gst.element_factory_make("playbin",None) self.bin.set_property("uri","file://"+self.info.filename) self.bin.set_state(gst.STATE_PAUSED) self.player.add(self.bin) # initialize bus bus = self.player.get_bus() bus.add_signal_watch() bus.connect("message",self.onMessage) self.playing = False self.hasFile = False # Initialize stuff for finding duration self.time_format = gst.Format(gst.FORMAT_TIME) # Called on bus messages def onMessage(self,bus,message): t = message.type if t == gst.MESSAGE_EOS: self.next(None,None) self.plause() elif t == gst.MESSAGE_ERROR: err, debug = message.parse_error() self.close() print "Error: %s" % err, debug print "Trying next song" self.next(None,None) # Toggle between play and pause def plause(self): if self.playing == False: self.player.set_state(gst.STATE_PLAYING) self.playing = True else: self.player.set_state(gst.STATE_PAUSED) self.playing = False # Stop playback def stop(self): self.player.set_state(gst.STATE_PAUSED) self.playing = False self.current = 0 #self.player.seek_simple(self.time_format,gst.SEEK_FLAG_FLUSH,self.current) self.seek(current) def seek(self,time): self.player.seek_simple(self.time_format,gst.SEEK_FLAG_FLUSH,time) # Close the song def close(self): self.player.set_state(gst.STATE_NULL) # Print out current running time def curTime(self): if self.playing == False: return (False,False) self.position = self.player.query_position(self.time_format,None)[0]/1000000000 return (True, self.position)