python: update python examples to modern python, Gst, and Glib versions
[experiments/gstreamer.git] / python / gst-playbin-switch.py
index 5333f53..0128a94 100755 (executable)
@@ -24,14 +24,13 @@ gi.require_version('Gst', '1.0')
 from gi.repository import Gst
 Gst.init(None)
 
-from gi.repository import GObject
-GObject.threads_init()
+from gi.repository import GLib
 
 
 class Player:
     def __init__(self):
-        self.loop = GObject.MainLoop()
-        self.pipeline = Gst.ElementFactory.make("playbin", "player")
+        self.loop = GLib.MainLoop()
+        self.pipeline = Gst.ElementFactory.make("playbin3", "player")
 
         files = ["sample_440hz.webm", "sample_880hz.webm"]
         self.uris = [Gst.filename_to_uri(f) for f in files]
@@ -58,7 +57,7 @@ class Player:
         self.pipeline.set_state(Gst.State.NULL)
         self.loop.quit()
 
-    def switch(self):
+    def on_switch(self):
         self.uri_index ^= 1
         print("Next: %s" % self.uris[self.uri_index])
 
@@ -94,10 +93,10 @@ def main():
 
     def stdin_cb(source, condition):
         source.readline()
-        player.switch()
+        player.on_switch()
         return True
 
-    GObject.io_add_watch(sys.stdin, GObject.IO_IN, stdin_cb)
+    GLib.io_add_watch(sys.stdin, GLib.IO_IN, stdin_cb)
 
     print("\nPress Enter to switch the source\n")
     player.run()