如何在raspberry pi中使用phonon + python + qt显示任何视频

时间:2017-02-11 05:47:30

标签: python pyqt raspberry-pi pyqt4 phonon

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

shell> mysql_tzinfo_to_sql /usr/share/zoneinfo

1 个答案:

答案 0 :(得分:1)

您必须使用以下命令为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