简体   繁体   English

使用Python,PyQt和Phonon播放mp3

[英]Play mp3 using Python, PyQt, and Phonon

I been trying all day to figure out the Qt's Phonon library with Python. 我一整天都想用Python来弄清楚Qt的Phonon库。

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. 我的长期目标是看看我是否可以让它播放mms://流,但由于我无法在任何地方找到这样做的实现,我将自己解决这个问题。 (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. 这将启动文件浏览器并播放指定的mp3文件。 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. 我想删除文件浏览器的东西并将其归结为执行脚本的基本要素并让它播放带有硬编码路径的Mp3文件。

I'm assuming my problem is a misunderstanding of setCurrentSource() and specifying the data types. 我假设我的问题是对setCurrentSource()的误解并指定了数据类型。 (see: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/phonon-mediasource.html#fileName ) (见: 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. 我保持我的问题有点广泛,因为理解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()

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. Phonon使用系统自己对媒体格式的支持,支持不同平台上的不同音频文件格式,因此可能是您的系统不提供播放MP3文件的库。 Certainly, MP3 is not supported out of the box on some Linux distributions. 当然,在某些Linux发行版中,不支持开箱即用的MP3。 If you are using Linux, please take a look at the following page for information about enabling MP3 support: 如果您使用的是Linux,请查看以下页面,了解有关启用MP3支持的信息:

http://doc.qt.io/qt-4.8/phonon-overview.html#linux 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: 另一种诊断Phonon媒体格式问题的方法是运行Qt提供的功能示例:

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

This should tell you which media formats are supported by your system. 这应该告诉您系统支持哪些媒体格式。

In delayedInit method; delayedInit方法中; create MediaObject like following: 创建MediaObject如下:

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

If Phonon is not outputting audio or video, but not throwing any errors. 如果Phonon没有输出音频或视频,但没有丢失任何错误。 You might just have to sudo apt-get install phonon-backend-gstreamer and also maybe sudo apt-get install libphonon-dev 您可能只需要sudo apt-get install phonon-backend-gstreamer ,也可以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. Phonon默默地使用gstreamer或vlc的后端,所以当它不存在时,没有错误但也没有功能。 after running those commands I was able to hear sound from phonon on my raspberry pi 在运行这些命令之后,我能够听到覆盆子pi上的声子发出的声音

Hopefully this will help someone in the future. 希望这将有助于未来的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM