pyqt: 使用PyQT制作视频播放器

转载自Python应用03 使用PyQT制作视频播放器

import time
import sys

from PyQt4 import QtGui, QtCore
from PyQt4.phonon import Phonon


class PollTimeThread(QtCore.QThread):
    """
    This thread works as a timer.
    """
    update = QtCore.pyqtSignal()

    def __init__(self, parent):
        super(PollTimeThread, self).__init__(parent)

    def run(self):
        while True:
            time.sleep(1)
            if self.isRunning():
                # emit signal
                self.update.emit()
            else:
                return

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        # media
        self.media = Phonon.MediaObject(self)
        self.media.stateChanged.connect(self.handleStateChanged)
        self.video = Phonon.VideoWidget(self)
        self.video.setMinimumSize(200, 200)
        self.audio = Phonon.AudioOutput(Phonon.VideoCategory, self)
        Phonon.createPath(self.media, self.audio)
        Phonon.createPath(self.media, self.video)

        # control button
        self.button = QtGui.QPushButton('选择文件', self)
        self.button.clicked.connect(self.handleButton)

        # for display of time lapse
        self.info = QtGui.QLabel(self)

        # layout
        layout = QtGui.QGridLayout(self)
        layout.addWidget(self.video, 1, 1, 3, 3)
        layout.addWidget(self.info, 4, 1, 1, 3)
        layout.addWidget(self.button, 5, 1, 1, 3)

        # signal-slot, for time lapse
        self.thread = PollTimeThread(self)
        self.thread.update.connect(self.update)

    def update(self):
        # slot
        lapse = self.media.currentTime()/1000.0
        self.info.setText("%4.2f 秒" % lapse)

    def startPlay(self):
        if self.path:
            self.media.setCurrentSource(Phonon.MediaSource(self.path))

            # use a thread as a timer
            self.thread = PollTimeThread(self)
            self.thread.update.connect(self.update)
            self.thread.start()
            self.media.play()

    def handleButton(self):
        if self.media.state() == Phonon.PlayingState:
            self.media.stop()
            self.thread.terminate()
        else:
            self.path = QtGui.QFileDialog.getOpenFileName(self, self.button.text())
            self.startPlay()

    def handleStateChanged(self, newstate, oldstate):
        if newstate == Phonon.PlayingState:
            self.button.setText('停止')
        elif (newstate != Phonon.LoadingState and
              newstate != Phonon.BufferingState):
            self.button.setText('选择文件')
            if newstate == Phonon.ErrorState:
                source = self.media.currentSource().fileName()
                print ('错误:不能播放:', source.toLocal8Bit().data())
                print ('  %s' % self.media.errorString().toLocal8Bit().data())


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('视频播放')
    window = Window()
    window.show()
    sys.exit(app.exec_())

[1] Python应用03 使用PyQT制作视频播放器

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值