Play mp3 using Python, PyQt, and Phonon

ε祈祈猫儿з 提交于 2019-12-03 09:57:47

问题


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

回答1:


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.




回答2:


In delayedInit method; create MediaObject like following:

def delayedInit(self):
    if not self.m_media:
       self.m_media = Phonon.createPlayer(Phonon.MusicCategory)



回答3:


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.



来源:https://stackoverflow.com/questions/1083118/play-mp3-using-python-pyqt-and-phonon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!