Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/phonon-mediasource.html#fileName)

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):
        QMainWindow.__init__(self)
        self.m_fileView = QColumnView(self)
        self.m_media = None

        self.setCentralWidget(self.m_fileView)
        self.m_fileView.setModel(self.m_model)
        self.m_fileView.setFrameStyle(QFrame.NoFrame)

        self.connect(self.m_fileView,
            SIGNAL("updatePreviewWidget(const QModelIndex &)"), self.play)

    def play(self, index):
        self.delayedInit()
        self.m_media.setCurrentSource(
            Phonon.MediaSource(self.m_model.filePath(index)))
        self.m_media.play()

    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()
    mw.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
like image 729
KeyboardInterrupt Avatar asked Jul 04 '09 22:07

KeyboardInterrupt


3 Answers

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:

http://doc.qt.io/qt-4.8/phonon-overview.html#linux

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

http://doc.qt.io/qt-4.8///qt-phonon-capabilities-example.html

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

like image 172
David Boddie Avatar answered Nov 18 '22 11:11

David Boddie


In delayedInit method; create MediaObject like following:

def delayedInit(self):
    if not self.m_media:
       self.m_media = Phonon.createPlayer(Phonon.MusicCategory)
like image 36
Gökmen Avatar answered Nov 18 '22 12:11

Gökmen


If Phonon is not outputting audio or video, but not throwing any errors. You might just have to sudo apt-get install phonon-backend-gstreamer and also maybe sudo apt-get install libphonon-dev

Phonon uses a backend of gstreamer or vlc silently, so when its not there, no errors but no functionality either. after running those commands I was able to hear sound from phonon on my raspberry pi

Hopefully this will help someone in the future.

like image 1
Prophacy Avatar answered Nov 18 '22 12:11

Prophacy