en
Aug 2008
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Last Played

» Che Sudaka – Cosmopolitan Time
» Manu Chao – Desaparecido
» Ska-P – Mestizaje
» Zebda – Du soleil a la toque
» Tryo – La lumiere
» La Vela Puerca – Hoy Tranquilo
» Los Cafres – Esta Puerta
» Le Maximum Kouette – C'est promu
» Amparanoia – Don;t Leave me Now
» Hot Pants – song for Laura today

Gstreamer based python audio player in no time

Gstreamer based python audio player in no time

I'm learning to develop with Gstreamer 0.10 framework, which natively comes with Python binding, which is rare enough to notice for a project.

So, starting an audio player is quite simple:

  • create a player from the GstElement factory
  • give your file uri to the player and set it to play
  • start a loop (i used gobject here, but it should work with gtk.main() or a while-true loop)

Here is the code:

import pygst
pygst.require('0.10')
import gst
import gobject, sys

def play_uri(uri):
    " play an uri like file:///home/foo/bar.mp3 "

    mainloop = gobject.MainLoop()
    player = gst.element_factory_make("playbin", "player")
  
    print 'Playing:', uri
    player.set_property('uri', uri)
    player.set_state(gst.STATE_PLAYING)

    mainloop.run()

Ok, this is a start, now i need to:

  • get status information
  • seek + / -
  • play/pause/stop
  • manage a playing queue
  • manage video playing

So things will be a bit more complicated in a moment ;)

by Philippe Normand on Fri Feb 24 12:40:39 2006 (Viewed: 6853 / 6 comments )
  |   RSS  |   RSS2  |   Atom  |   Source  |   Edit

#.   Jkx on Fri Feb 24 13:13:25 2006

Does this mean you need to play w/ the mainloop nightmare (ala Gtk) ? The sound doesn't play in background ?

This is kind of stuff, that is pretty easy to do in plain Old C fashion w/ thread and other stuff, but that became a pain in the ass in python. From want I know (little experience w/ that kind of stuff), this should play the music in background with polling a loop. (Not even talking the wrong effec this can have when you have to deal w/ a bunch of mainloop() .. gtk + gstream + twisted .. )

#.   Jkx on Fri Feb 24 13:14:26 2006

s/with polling/without polling/g

#.   zgoda on Fri Feb 24 15:37:16 2006
All of examples from gst-python distribution use some kind of while loop. Seems that GStreamer has no own loop itself.
#.   phil on Fri Feb 24 16:07:47 2006

From what i understood, Gstreamer has its own "playing thread", and to keep it alive from your code, you have to perform a long task (like time.sleep(song_duration) for instance)

#.   Johan Dahlin on Fri Feb 24 16:59:25 2006

You'll only need a mainloop if you want to send messages via the bus. You can use a GObject mainloop or a Gtk+ one depending on the application you're writing. Eg, if you're not writing a graphical gtk+ application, the gobject one is recommended. (You could also write your own mainloop, since in 0.10 the only thing it's used for is to send messages)

#.   patrickkidd on Fri Feb 24 20:46:42 2006

The examples in gst-python are for 0.8, which had a completely different threading model. Everything happens in the background now, and as John said, you only need the main loop if you want to get gobject messages from the bus. Check here for an example: https://svn.patrickkidd.com/pk/trac/wiki/QGObjectLoop

Comments not allowed anymore on this post