Add gst-player-example.py
authorAntonio Ospite <ao2@ao2.it>
Fri, 2 Dec 2016 11:57:49 +0000 (12:57 +0100)
committerAntonio Ospite <ao2@ao2.it>
Fri, 2 Dec 2016 11:57:49 +0000 (12:57 +0100)
python/gst-player-example.py [new file with mode: 0755]

diff --git a/python/gst-player-example.py b/python/gst-player-example.py
new file mode 100755 (executable)
index 0000000..b7b838e
--- /dev/null
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+#
+# Player - a very simple media player based on GstPlayer
+#
+# Copyright (C) 2016  Antonio Ospite <ao2@ao2.it>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import sys
+import gi
+
+gi.require_version('Gst', '1.0')
+from gi.repository import Gst
+Gst.init(None)
+
+gi.require_version('GstPlayer', '1.0')
+from gi.repository import GstPlayer
+
+from gi.repository import GObject
+GObject.threads_init()
+
+
+class Player(object):
+    def __init__(self):
+        self.player = GstPlayer.Player.new(signal_dispatcher=GstPlayer.PlayerGMainContextSignalDispatcher())
+        self.player.connect("error", self.error_cb)
+        self.player.connect("end-of-stream", self.end_of_stream_cb)
+        self.player.connect("state-changed", self.state_changed_cb)
+
+        self.mainloop = GObject.MainLoop()
+
+    def end_of_stream_cb(self, player):
+        self.mainloop.quit()
+
+    def error_cb(self, player, error):
+        print(error)
+        self.mainloop.quit()
+
+    def state_changed_cb(self, player, state):
+        print(state)
+
+    def play(self, filename):
+        self.player.set_uri(Gst.filename_to_uri(filename))
+        self.player.play()
+        self.mainloop.run()
+
+    def stop(self):
+        self.player.stop()
+        self.mainloop.quit()
+
+
+def main():
+    if len(sys.argv) > 1:
+        player = Player()
+        try:
+            player.play(sys.argv[1])
+        except KeyboardInterrupt:
+            player.stop()
+
+
+if __name__ == "__main__":
+    main()