使用 Python、PyQt 和 Phonon 播放 mp3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1083118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Play mp3 using Python, PyQt, and Phonon
提问by KeyboardInterrupt
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. (figured I'd put it out there if anyone knew more about this specifically, if not no big deal.)
我的长期目标是看看我是否可以让它播放 mms:// 流,但由于我找不到在任何地方完成的实现,我会自己弄清楚那部分。(我想如果有人对此有更多了解的话,我会把它放在那里,如果不是什么大不了的话。)
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.
无论如何,我想我会从我在网上找到的一个工作示例向后工作。这将启动文件浏览器并播放指定的 mp3 文件。我想去掉文件浏览器的内容,并将其归结为执行脚本的基本要素,并让它播放带有硬编码路径的 Mp3 文件。
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)
我假设我的问题是对 setCurrentSource() 和指定数据类型的误解。(参见: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()
回答by David Boddie
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:
Phonon 在不同平台上支持不同的音频文件格式,使用系统自身对媒体格式的支持,因此可能是您的系统没有提供播放 MP3 文件的库。当然,某些 Linux 发行版不支持开箱即用的 MP3。如果您使用的是 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 提供的 Capabilities 示例:
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.
这应该会告诉您系统支持哪些媒体格式。
回答by G?kmen
In delayedInit
method; create MediaObject
like following:
在delayedInit
方法中;创建MediaObject
如下:
def delayedInit(self):
if not self.m_media:
self.m_media = Phonon.createPlayer(Phonon.MusicCategory)
回答by Prophacy
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 没有输出音频或视频,但没有抛出任何错误。你可能只需要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. after running those commands I was able to hear sound from phonon on my raspberry pi
Phonon 默默地使用 gstreamer 或 vlc 的后端,所以当它不存在时,没有错误但也没有功能。运行这些命令后,我能够听到树莓派上声子的声音
Hopefully this will help someone in the future.
希望这会在将来对某人有所帮助。