gpt4 book ai didi

python - 如何在树莓派中使用 phonon + python + qt 显示任何视频

转载 作者:太空宇宙 更新时间:2023-11-03 15:40:37 25 4
gpt4 key购买 nike

我有树莓派3。我想使用 phonon+qt+python 显示任何类型的视频。我不想在 QT 中使用 c++。我曾尝试使用以下代码,但它给出的错误如下:没有名为 phonon 的模块。我已经使用 sudo apt-get install phonon 安装 phonon,但仍然出现这样的错误。任何人都知道如何解决这个问题,或者有没有其他方法可以使用 python + qt 显示视频,或者有其他方法可以安装 phonon?

from PyQt4 import QtGui, QtCore
import PyQt4.phonon

class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.media = phonon.MediaObject(self)
self.media.stateChanged.connect(self.handleStateChanged)
self.video = phonon.VideoWidget(self)
self.video.setMinimumSize(400, 400)
self.audio = phonon.AudioOutput(phonon.VideoCategory, self)
phonon.createPath(self.media, self.audio)
phonon.createPath(self.media, self.video)
self.button = QtGui.QPushButton('Choose File', self)
self.button.clicked.connect(self.handleButton)
self.list = QtGui.QListWidget(self)
self.list.addItems(phonon.BackendCapabilities.availableMimeTypes())
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.video, 1)
layout.addWidget(self.button)
layout.addWidget(self.list)

def handleButton(self):
if self.media.state() == phonon.PlayingState:
self.media.stop()
else:
path = QtGui.QFileDialog.getOpenFileName(self, self.button.text())
if path:
self.media.setCurrentSource(phonon.MediaSource(path))
self.media.play()

def handleStateChanged(self, newstate, oldstate):
if newstate == phonon.PlayingState:
self.button.setText('Stop')
elif (newstate != phonon.LoadingState and
newstate != phonon.BufferingState):
self.button.setText('Choose File')
if newstate == phonon.ErrorState:
source = self.media.currentSource().fileName()
print ('ERROR: could not play:', source.toLocal8Bit().data())
print (' %s' % self.media.errorString().toLocal8Bit().data())

if __name__ == '__main__':

import sys
app = QtGui.QApplication(sys.argv)
app.setApplicationName('phonon Player')
window = Window()
window.show()
sys.exit(app.exec_())

最佳答案

您必须使用以下命令为 pyqt 安装 phonon:

sudo apt-get install python-qt4-phonon

此外,您必须更正某些导入和部分代码:

from PyQt4 import QtGui
from PyQt4.phonon import Phonon


class Window(QtGui.QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent=parent)
self.media = Phonon.MediaObject(self)
self.video = Phonon.VideoWidget(self)
self.video.setMinimumSize(400, 400)
self.audio = Phonon.AudioOutput(Phonon.VideoCategory, self)
Phonon.createPath(self.media, self.audio)
Phonon.createPath(self.media, self.video)
self.button = QtGui.QPushButton('Choose File', self)

self.list = QtGui.QListWidget(self)
self.list.addItems(Phonon.BackendCapabilities.availableMimeTypes())
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.video, 1)
layout.addWidget(self.button)
layout.addWidget(self.list)

self.media.stateChanged.connect(self.handleStateChanged)
self.button.clicked.connect(self.handleButton)

def handleButton(self):
if self.media.state() == Phonon.PlayingState:
self.media.stop()
else:
path = QtGui.QFileDialog.getOpenFileName(self, self.button.text())
if path:
self.media.setCurrentSource(Phonon.MediaSource(path))
self.media.play()

def handleStateChanged(self, newstate, oldstate):
if newstate == Phonon.PlayingState:
self.button.setText('Stop')
elif newstate != Phonon.LoadingState and newstate != Phonon.BufferingState:
self.button.setText('Choose File')
if newstate == Phonon.ErrorState:
source = self.media.currentSource().fileName()
print('ERROR: could not play:', source.toLocal8Bit().data())
print(' %s' % self.media.errorString().toLocal8Bit().data())


if __name__ == '__main__':
import sys

app = QtGui.QApplication(sys.argv)
app.setApplicationName('phonon Player')
window = Window()
window.show()
sys.exit(app.exec_())

屏幕截图:

enter image description here

关于python - 如何在树莓派中使用 phonon + python + qt 显示任何视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42172709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com