106 lines
1.6 KiB
Python
106 lines
1.6 KiB
Python
# Bryan Schumaker (8/7/2010)
|
|
|
|
import libsaria
|
|
|
|
gst = None
|
|
player = None
|
|
time = None
|
|
bus = None
|
|
length = None
|
|
|
|
# An init function for faster startup
|
|
def init():
|
|
global gst
|
|
global player
|
|
global time
|
|
import gst
|
|
|
|
player = gst.element_factory_make("playbin2", "player")
|
|
time = gst.Format(gst.FORMAT_TIME)
|
|
bus = player.get_bus()
|
|
bus.add_signal_watch()
|
|
bus.connect("message", on_message)
|
|
|
|
|
|
def reset():
|
|
global player
|
|
global length
|
|
player.set_state(gst.STATE_NULL)
|
|
length = None
|
|
|
|
|
|
def on_message(bus, message):
|
|
if message.type == gst.MESSAGE_EOS:
|
|
libsaria.music.end_of_song()
|
|
|
|
|
|
def on_quit():
|
|
global player
|
|
player.set_state(gst.STATE_NULL)
|
|
libsaria.event.invite("PREQUIT", on_quit)
|
|
|
|
|
|
def get_state():
|
|
global player
|
|
return player.get_state()[1]
|
|
|
|
|
|
# Returns the length of the song in ms
|
|
def duration():
|
|
global length
|
|
global player
|
|
global time
|
|
if get_state() == gst.STATE_NULL:
|
|
return 0
|
|
if length == None:
|
|
length = player.query_duration(time)[0]
|
|
return length
|
|
|
|
|
|
def position():
|
|
global time
|
|
global player
|
|
return player.query_position(time)[0]
|
|
|
|
|
|
def load(file):
|
|
global gst
|
|
global player
|
|
player.set_state(gst.STATE_NULL)
|
|
player.set_property("uri", "file://%s"%file)
|
|
player.set_state(gst.STATE_PAUSED)
|
|
return file
|
|
|
|
|
|
def play():
|
|
global gst
|
|
global player
|
|
player.set_state(gst.STATE_PLAYING)
|
|
|
|
|
|
def pause():
|
|
global gst
|
|
global player
|
|
player.set_state(gst.STATE_PAUSED)
|
|
|
|
|
|
def stop():
|
|
pause()
|
|
seek(0)
|
|
|
|
|
|
def seek(prcnt):
|
|
global player
|
|
global time
|
|
global gst
|
|
try:
|
|
spot = duration() * prcnt
|
|
player.seek_simple(time, gst.SEEK_FLAG_FLUSH, spot)
|
|
except:
|
|
pass
|
|
|
|
|
|
def volume(prcnt):
|
|
global player
|
|
player.set_property("volume", prcnt)
|