ocarina/trunk/song.py

80 lines
1.8 KiB
Python

import sys
import time
import pygst
pygst.require("0.10")
import gst
from duration import Duration
class Song():
def __init__(self,file,exitFunc,prnt):
self.quit=exitFunc
self.prnt=prnt
# 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
# Called on bus messages
def onMessage(self,bus,message):
t = message.type
if t == gst.MESSAGE_EOS:
#print "End of stream"
self.prnt("End of stream")
self.quit()
elif t == gst.MESSAGE_ERROR:
err, debug = message.parse_error()
self.prnt("Error: %s" % err,debug)
#print "Error: %s" % err, debug
self.quit()
elif t == gst.MESSAGE_TAG:
#print "Tags:"
# Probably should convert this to some other data structure
# For easier use later
taglist = message.parse_tag()
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(self.prnt)
# Print out current running time
def curTime(self):
length = self.player.query_position(self.time_format,None)[0]
dur = Duration()
dur.setTime(length)
return dur