#! /usr/bin/env python
# Pass files to play as arguments.

import os
import sys

import gst
import gobject
gobject.threads_init()

class Player:
    def __init__(self, files):
        self.files = files

        self.bin = gst.element_factory_make('playbin')
        self.bin.set_property('video-sink', None)
        try:
            device = gst.parse_launch('alsasink')
        except gobject.GError:
            pass
        else:
            self.bin.set_property('audio-sink', device)

        bus = self.bin.get_bus()
        bus.add_signal_watch()
        bus.connect('message::eos', lambda bus, msg: self.play_next())

    def play_next(self):
        try:
            uri = 'file://' + os.path.abspath(self.files.pop(0))
        except IndexError:
            sys.exit(0)
        else:
            self.bin.set_property('uri', uri)
            self.bin.set_state(gst.STATE_NULL)
            self.bin.set_state(gst.STATE_PLAYING)

player = Player(sys.argv[1:])
player.play_next()
gobject.MainLoop().run()

