SHARE
TWEET

Python, GStreamer, and gi.repository - source

a guest Feb 7th, 2016 273 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import gi
  3. gi.require_version('Gst', '1.0')
  4. from gi.repository import GObject
  5. from gi.repository import Gst
  6. from gi.repository import Gtk
  7.  
  8.  
  9. GObject.threads_init()
  10. Gst.init(None)
  11.  
  12.  
  13.  
  14. class Main:
  15.     def __init__(self):
  16.         # Create gui bits and bobs
  17.        
  18.         builder = Gtk.Builder()
  19.         builder.add_from_file('sound.glade')
  20.         self.window = builder.get_object("mainwindow")
  21.  
  22.         signals = {
  23.             "on_play_clicked" : self.OnPlay,
  24.             "on_stop_clicked" : self.OnStop,
  25.             "on_quit_clicked" : self.OnQuit,
  26.         }
  27.  
  28.         builder.connect_signals(signals)
  29.        
  30.         # Create GStreamer bits and bobs
  31.  
  32.         self.pipeline = Gst.Pipeline("mypipeline")
  33.  
  34.         self.audiotestsrc = Gst.ElementFactory.make("audiotestsrc", "audio")
  35.         self.audiotestsrc.set_property("freq", 440)
  36.         self.pipeline.add(self.audiotestsrc)
  37.  
  38.         self.sink = Gst.ElementFactory.make("alsasink", "sink")
  39.         self.pipeline.add(self.sink)
  40.  
  41.         self.audiotestsrc.link(self.sink)
  42.  
  43.         self.window.show_all()
  44.  
  45.     def OnPlay(self, widget):
  46.         print("play")
  47.         self.pipeline.set_state(Gst.State.PLAYING)
  48.  
  49.     def OnStop(self, widget):
  50.         print("stop")
  51.         #self.pipeline.set_state(Gst.State.READY)
  52.         self.pipeline.set_state(Gst.State.PAUSED)
  53.  
  54.     def OnQuit(self, widget):
  55.         Gtk.main_quit()
  56.  
  57. start=Main()
  58. Gtk.main()
  59.  
  60.  
  61. """
  62. Use with the following sound.glade file:
  63.  
  64. <?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
  65. <!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
  66.  
  67. <interface>
  68.  
  69. <object class="GtkWindow" id="mainwindow">
  70.  <property name="visible">True</property>
  71.  <property name="title" translatable="yes">window1</property>
  72.  <property name="type">GTK_WINDOW_TOPLEVEL</property>
  73.  <property name="window_position">GTK_WIN_POS_NONE</property>
  74.  <property name="modal">False</property>
  75.  <property name="resizable">True</property>
  76.  <property name="destroy_with_parent">False</property>
  77.  <property name="decorated">True</property>
  78.  <property name="skip_taskbar_hint">False</property>
  79.  <property name="skip_pager_hint">False</property>
  80.  <property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
  81.  <property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
  82.  <property name="focus_on_map">True</property>
  83.  <property name="urgency_hint">False</property>
  84.  
  85.  <child>
  86.    <object class="GtkHButtonBox" id="hbuttonbox1">
  87.      <property name="visible">True</property>
  88.      <property name="layout_style">GTK_BUTTONBOX_DEFAULT_STYLE</property>
  89.      <property name="spacing">0</property>
  90.  
  91.      <child>
  92.     <object class="GtkButton" id="button1">
  93.       <property name="visible">True</property>
  94.       <property name="can_default">True</property>
  95.       <property name="can_focus">True</property>
  96.       <property name="label">gtk-media-play</property>
  97.       <property name="use_stock">True</property>
  98.       <property name="relief">GTK_RELIEF_NORMAL</property>
  99.       <property name="focus_on_click">True</property>
  100.       <signal name="clicked" handler="on_play_clicked" last_modification_time="Sat, 20 May 2006 22:35:59 GMT"/>
  101.     </object>
  102.      </child>
  103.  
  104.      <child>
  105.     <object class="GtkButton" id="button2">
  106.       <property name="visible">True</property>
  107.       <property name="can_default">True</property>
  108.       <property name="can_focus">True</property>
  109.       <property name="label">gtk-media-stop</property>
  110.       <property name="use_stock">True</property>
  111.       <property name="relief">GTK_RELIEF_NORMAL</property>
  112.       <property name="focus_on_click">True</property>
  113.       <signal name="clicked" handler="on_stop_clicked" last_modification_time="Sat, 20 May 2006 22:36:08 GMT"/>
  114.     </object>
  115.      </child>
  116.  
  117.      <child>
  118.     <object class="GtkButton" id="button3">
  119.       <property name="visible">True</property>
  120.       <property name="can_default">True</property>
  121.       <property name="can_focus">True</property>
  122.       <property name="label">gtk-quit</property>
  123.       <property name="use_stock">True</property>
  124.       <property name="relief">GTK_RELIEF_NORMAL</property>
  125.       <property name="focus_on_click">True</property>
  126.       <signal name="clicked" handler="on_quit_clicked" last_modification_time="Sat, 20 May 2006 22:36:21 GMT"/>
  127.     </object>
  128.      </child>
  129.    </object>
  130.  </child>
  131. </object>
  132.  
  133. </interface>
  134. """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top