81 lines
1.5 KiB
Python
81 lines
1.5 KiB
Python
# Bryan Schumaker (2 / 6 / 2011)
|
|
# NOTE: DO NOT CALL FUNCTIONS IN THIS FILE DIRECTLY
|
|
# USE THE FUNCTIONS IN __init__.py INSTEAD. THIS
|
|
# WILL ENSURE THAT THE AUDIO LOCK IS HELD :-)
|
|
|
|
import gst
|
|
|
|
player = gst.element_factory_make("playbin2", "player")
|
|
time = gst.Format(gst.FORMAT_TIME)
|
|
bus = player.get_bus()
|
|
bus.add_signal_watch()
|
|
|
|
cur_file = None
|
|
|
|
def reset():
|
|
global cur_file
|
|
cur_file = None
|
|
player.set_state(gst.STATE_NULL)
|
|
|
|
def load_file(type, file):
|
|
global cur_file
|
|
reset()
|
|
cur_file = file
|
|
player.set_property("uri", "%s://%s" % (type, file))
|
|
|
|
def get_cur_file():
|
|
return cur_file
|
|
|
|
def has_file():
|
|
return cur_file != None
|
|
|
|
def change_state(state):
|
|
if has_file() == True:
|
|
player.set_state(state)
|
|
else:
|
|
player.set_state(gst.STATE_NULL)
|
|
|
|
def play():
|
|
change_state(gst.STATE_PLAYING)
|
|
|
|
def pause():
|
|
change_state(gst.STATE_PAUSED)
|
|
|
|
def stop():
|
|
pause()
|
|
seek_to(0)
|
|
|
|
def get_state():
|
|
return player.get_state()[1]
|
|
|
|
def position():
|
|
if get_state() == gst.STATE_NULL:
|
|
pos = 0.0
|
|
else:
|
|
pos = player.query_position(time)[0]
|
|
return float(pos)
|
|
|
|
def duration():
|
|
if get_state() == gst.STATE_NULL:
|
|
dur = 0.0
|
|
else:
|
|
dur = player.query_duration(time)[0]
|
|
return float(dur)
|
|
|
|
def seek_to(new_spot):
|
|
if new_spot < 0:
|
|
new_spot = 0
|
|
player.seek_simple(time, gst.SEEK_FLAG_FLUSH, new_spot)
|
|
|
|
def seek(amount):
|
|
pos = position()
|
|
if pos == 0.0:
|
|
return
|
|
seek_to( pos + (amount * 1000000000) )
|
|
|
|
def seek_prcnt(prcnt):
|
|
seek_to(prcnt * duration())
|
|
|
|
def set_volume(prcnt):
|
|
player.set_property("volume", prcnt)
|