import sys, os
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst

Gst.init(None)

ogg_uri  = "file:///home/tpm/a.ogg"
flac_uri = "file:///home/tpm/b.flac"

def about_to_finish(player):
    print "about to finish, set flac as next song"
    player.set_property("uri", flac_uri)

player = Gst.ElementFactory.make("playbin", "player")
player.connect('about-to-finish', about_to_finish)
player.set_property("uri", ogg_uri)

GST_CLOCK_TIME_NONE = 18446744073709551615

def start():
    print "start"
    print player.set_state(Gst.State.PAUSED)
    print player.get_state(GST_CLOCK_TIME_NONE)
    duration = player.query_duration(Gst.Format.TIME)[1]
    print duration / Gst.SECOND
    print "seek to 5 seconds before the ogg finishes"
    player.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH, duration - Gst.SECOND * 5)
    print player.set_state(Gst.State.PLAYING)
    return False

def print_position():
    print player.get_state(GST_CLOCK_TIME_NONE)[1].value_name,
    print player.query_position(Gst.Format.TIME)[1] / Gst.SECOND,
    print player.get_property("current-uri")
    return True

GObject.idle_add(start)
GObject.timeout_add(1000, print_position)

loop = GObject.MainLoop(None)
loop.run()

