Wednesday, 16 May 2012

PyQt4 and audio playback with phonon

So today I wanted to try and use PyQt4 to play music. I couldn't find an example.. so here goes. first.. you need some obscure(ish) dependencies.
~$ sudo apt-get install python-qt4-phonon phonon-backend-gstreamer
then the following code should work. (choose your own music file of course!)
import sys
from PyQt4 import QtCore
from PyQt4.phonon import Phonon

app = QtCore.QCoreApplication(sys.argv)
app.setApplicationName("my_player") #phonon useage requires an application name

m = Phonon.MediaSource("/home/neil/Music/pop/ABC - All of My Heart.mp3")
obj = Phonon.createPlayer(Phonon.MusicCategory, m)
obj.play()

app.exec_()