Play mp3 using Python, PyQt, and Phonon

Posted by KeyboardInterrupt on Stack Overflow See other posts from Stack Overflow or by KeyboardInterrupt
Published on 2009-07-04T22:47:08Z Indexed on 2010/04/01 20:33 UTC
Read the original article Hit count: 795

Filed under:
|
|
|

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()

© Stack Overflow or respective owner

Related posts about python

Related posts about pyqt