Python – Play mp3 using Python, PyQt, and Phonon


I been trying all day to figure out the Qt's Phonon library with Python.

My long term goal is to see if I could get it to play a mms:// stream, but since I can't find an implementation of this done anywhere, I will figure that part out myself. (figured I'd put it out there if anyone knew more about this specifically, if not no big deal.)

Anyway, I figured I'd work backwards from a working example I found online. This launches a file browser and will play the mp3 file specified. I wanted to strip out the file browser stuff and get it down to the essentials of executing the script and having it play an Mp3 file with a hardcoded path.

I'm assuming my problem is a misunderstanding of setCurrentSource() and specifying the data types. (see:

I'm keeping my question kind of broad because ANY help with understanding Phonon would be greatly appreciated.

import sys

from PyQt4.QtGui import QApplication, QMainWindow, QDirModel, QColumnView
from PyQt4.QtGui import QFrame
from PyQt4.QtCore import SIGNAL
from PyQt4.phonon import Phonon

class MainWindow(QMainWindow):

    m_model = QDirModel()

    def __init__(self):
        self.m_fileView = QColumnView(self)
        self.m_media = None


            SIGNAL("updatePreviewWidget(const QModelIndex &)"),

    def play(self, index):

    def delayedInit(self):
        if not self.m_media:
            self.m_media = Phonon.MediaObject(self)
            audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
            Phonon.createPath(self.m_media, audioOutput)

def main():
    app = QApplication(sys.argv)
    QApplication.setApplicationName("Phonon Tutorial 2 (Python)")
    mw = MainWindow()

if __name__ == '__main__':

Best Solution

Phonon supports different audio file formats on different platforms, using the system's own support for media formats, so it could be that your system doesn't provide libraries for playing MP3 files. Certainly, MP3 is not supported out of the box on some Linux distributions. If you are using Linux, please take a look at the following page for information about enabling MP3 support:

Another way to diagnose problems with Phonon's media formats is to run the Capabilities example provided with Qt:

This should tell you which media formats are supported by your system.

Related Question