How to play a simple song using PyQt's Phonon?

jonathan.hepp picture jonathan.hepp · Nov 8, 2011 · Viewed 8k times · Source

I'm trying to play a single mp3 file on my software when a button is pressed. Here's how i did it:

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), playsong)

and the function:

def playsong():            
        m_media = Phonon.MediaObject()
        m_media.setCurrentSource(Phonon.MediaSource("files/song.mp3"))
        m_media.play()

This doesnt raise any error. But the song wont play. I've seen a lot of exemples in C++ and some in python which has a lot of songs, and playlist, etc. I just want to play a single song, am i missing something?

Answer

Fábio Diniz picture Fábio Diniz · Nov 8, 2011

I use phonon on pyqt and my code has a few more lines:

output = Phonon.AudioOutput(Phonon.MusicCategory)
m_media = Phonon.MediaObject()
Phonon.createPath(m_media, output)
m_media.setCurrentSource(Phonon.MediaSource("files/song.mp3"))
m_media.play()

But honestly, I've found out that phonon is not able to play some specific songs with weird ID3 tags, so I've switched to pyaudiere (https://pypi.python.org/pypi/pyaudiere), which is much more stable (but yes, its less integrated with Qt).