简体   繁体   English

在PyQt中播放mov文件

[英]Play mov files in PyQt

I'd like to open a mov file in my PyQt application with a Quicktime-like player. 我想在我的PyQt应用程序中使用类似Quicktime的播放器打开一个mov文件。

I am trying to use the phonon module but somehow I can't get it to work. 我正在尝试使用声子模块,但不知怎的,我无法让它工作。

In my ui file (generated by Qt designer) it is initialized like this : 在我的ui文件(由Qt设计器生成)中,它被初始化为:

self.videoPlayer = phonon.Phonon.VideoPlayer(self.gridLayoutWidget)
self.videoPlayer.setObjectName(_fromUtf8("videoPlayer"))

And in my code I try this : 在我的代码中我试试这个:

media_source = phonon.Phonon.MediaSource("path\\to\\my\\media.mov")
self.ui.videoPlayer.load(media_source)
self.ui.videoPlayer.play()

Can someone point me out where I'm doing something wrong ? 有人能指出我在哪里做错了吗?

I don't see anything obviously wrong with the code you posted. 我发布的代码没有看到任何明显错误。 If your system has the necessary codecs available, there's no reason why phonon shouldn't be able to play quicktime files. 如果您的系统具有必要的编解码器,则没有理由说为什么声子不能播放quicktime文件。

Below is a simple phonon demo that plays quicktime files for me (on Linux, using the GStreamer backend). 下面是一个简单的声子演示,可以为我播放quicktime文件(在Linux上,使用GStreamer后端)。 The demo also lists all the available mime-types that the current phonon backend can handle. 该演示还列出了当前声子后端可以处理的所有可用的mime类型。 On my system, the mime-type "video/quicktime" is shown in the list. 在我的系统上,mime类型的“video / quicktime”显示在列表中。

If you run it in a console, it will also print out any phonon error messages. 如果您在控制台中运行它,它也会打印出任何声音错误消息。

EDIT 编辑

It appears that, on windows, phonon may use Windows Media Player as the backend. 看来,在Windows上,声子可能会使用Windows Media Player作为后端。 If so, then it may be necessary to install some extra codecs for WMP so that it can play quicktime files. 如果是这样,那么可能需要为WMP安装一些额外的编解码器,以便它可以播放quicktime文件。

from PyQt4 import QtGui, QtCore
from PyQt4.phonon import 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_())

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

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