使用Python、PyQt和Phonon播放mp3

7
我一整天都在尝试使用Python解决Qt的Phonon库问题。
我的长期目标是看看能否使其播放mms://流,但由于我找不到任何已经实现的方案,所以我将自己解决这部分内容。(如果有人对此特别了解,那么请告诉我,如果没有也没关系。)
无论如何,我想从我在网上找到的工作示例开始倒推。这会启动一个文件浏览器并播放指定的mp3文件。我想剥离掉文件浏览器部分,将其简化为执行脚本并使用硬编码路径播放Mp3文件的基本要素。
我认为我的问题是setCurrentSource()和指定数据类型的误解。(参见:http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/phonon-mediasource.html#fileName
我保持我的问题有点宽泛,因为任何有关理解Phonon的帮助都将不胜感激。
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()
3个回答

3
Phonon在不同平台上支持不同的音频文件格式,使用系统自己对媒体格式的支持,因此可能您的系统没有提供播放MP3文件的库。当然,某些Linux发行版默认情况下不支持MP3。如果您正在使用Linux,请查看以下页面以获取有关启用MP3支持的信息:

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

另一种诊断 Phonon 媒体格式问题的方法是运行 Qt 提供的 Capabilities 示例:

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

这应该告诉您的系统支持哪些媒体格式。

1
delayedInit方法中,创建以下类似的MediaObject
def delayedInit(self):
    if not self.m_media:
       self.m_media = Phonon.createPlayer(Phonon.MusicCategory)

1
如果Phonon没有输出音频或视频,但没有抛出任何错误。您可能只需要sudo apt-get install phonon-backend-gstreamer,也可能需要sudo apt-get install libphonon-dev
Phonon默默地使用gstreamer或vlc后端,所以当它不存在时,既没有错误也没有功能。 运行这些命令后,我能够在我的树莓派上听到来自Phonon的声音。
希望这能帮助未来的某个人。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接