使用Python,PyQt和Phonon播放mp3 [英] Play mp3 using Python, PyQt, and Phonon

查看:124
本文介绍了使用Python,PyQt和Phonon播放mp3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在尝试用Python找出Qt的Phonon库.

I been trying all day to figure out the Qt's Phonon library with Python.

我的长远目标是看是否可以播放mms://流,但是由于找不到在任何地方完成的实现,因此我会自己解决这一问题. (如果有人对此有更多的了解,我会把它放在那里,如果不是什么大问题的话.)

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

无论如何,我认为我会从网上找到的工作示例中倒退.这将启动文件浏览器,并将播放指定的mp3文件.我想剥离文件浏览器的内容,并深入到执行脚本并让其播放带有硬编码路径的Mp3文件的本质.

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.

我假设我的问题是对setCurrentSource()的误解,并指定了数据类型. (请参阅: http://www.riverbankcomputing. co.uk/static/Docs/PyQt4/html/phonon-mediasource.html#fileName )

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

推荐答案

Phonon使用系统自身对媒体格式的支持,在不同平台上支持不同的音频文件格式,因此可能是您的系统未提供用于播放的库MP3文件.当然,在某些Linux发行版中不立即支持MP3.如果您使用的是Linux,请查看以下页面,以获取有关启用MP3支持的信息:

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

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

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.

这篇关于使用Python,PyQt和Phonon播放mp3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆