ocarina/trunk/src/song.py

84 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,info,exitFunc,prnt):
def __init__(self,exitFunc):
self.quit=exitFunc
# initialize player pipeline
self.next = None
self.player = gst.Pipeline("player")
self.bin = gst.element_factory_make("playbin",None)
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.hasFile == False:
return
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):
if self.hasFile == False:
return
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)
# Close the song
def close(self):
self.playing = False
self.player.set_state(gst.STATE_NULL)
# Print out current running time
def curTime(self):
if self.playing == False:
return (False,False)
return (True, self.player.query_position(self.time_format,None)[0])
# Use to load a file path
def passInfo(self,info):
self.info = info
self.bin.set_property("uri","file://"+self.info.filename)
self.hasFile = True