zoukankan      html  css  js  c++  java
  • pyqt5播放视频,flash

    搜集的资料没试过

    例子1

    #!/usr/bin/env python
    
    
    #############################################################################
    ##
    ## Copyright (C) 2013 Riverbank Computing Limited.
    ## Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
    ## All rights reserved.
    ##
    ## This file is part of the examples of PyQt.
    ##
    ## $QT_BEGIN_LICENSE:BSD$
    ## You may use this file under the terms of the BSD license as follows:
    ##
    ## "Redistribution and use in source and binary forms, with or without
    ## modification, are permitted provided that the following conditions are
    ## met:
    ##   * Redistributions of source code must retain the above copyright
    ##     notice, this list of conditions and the following disclaimer.
    ##   * Redistributions in binary form must reproduce the above copyright
    ##     notice, this list of conditions and the following disclaimer in
    ##     the documentation and/or other materials provided with the
    ##     distribution.
    ##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
    ##     the names of its contributors may be used to endorse or promote
    ##     products derived from this software without specific prior written
    ##     permission.
    ##
    ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    ## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    ## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    ## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    ## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    ## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    ## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
    ## $QT_END_LICENSE$
    ##
    #############################################################################
    
    
    from PyQt5.QtCore import QDir, Qt, QUrl
    from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
    from PyQt5.QtMultimediaWidgets import QVideoWidget
    from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
            QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
    
    
    class VideoPlayer(QWidget):
    
        def __init__(self, parent=None):
            super(VideoPlayer, self).__init__(parent)
    
            self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
    
            videoWidget = QVideoWidget()
    
            openButton = QPushButton("Open...")
            openButton.clicked.connect(self.openFile)
    
            self.playButton = QPushButton()
            self.playButton.setEnabled(False)
            self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
            self.playButton.clicked.connect(self.play)
    
            self.positionSlider = QSlider(Qt.Horizontal)
            self.positionSlider.setRange(0, 0)
            self.positionSlider.sliderMoved.connect(self.setPosition)
    
            self.errorLabel = QLabel()
            self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
                    QSizePolicy.Maximum)
    
            controlLayout = QHBoxLayout()
            controlLayout.setContentsMargins(0, 0, 0, 0)
            controlLayout.addWidget(openButton)
            controlLayout.addWidget(self.playButton)
            controlLayout.addWidget(self.positionSlider)
    
            layout = QVBoxLayout()
            layout.addWidget(videoWidget)
            layout.addLayout(controlLayout)
            layout.addWidget(self.errorLabel)
    
            self.setLayout(layout)
    
            self.mediaPlayer.setVideoOutput(videoWidget)
            self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
            self.mediaPlayer.positionChanged.connect(self.positionChanged)
            self.mediaPlayer.durationChanged.connect(self.durationChanged)
            self.mediaPlayer.error.connect(self.handleError)
    
        def openFile(self):
            fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
                    QDir.homePath())
    
            if fileName != '':
                self.mediaPlayer.setMedia(
                        QMediaContent(QUrl.fromLocalFile(fileName)))
                self.playButton.setEnabled(True)
    
        def play(self):
            if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
                self.mediaPlayer.pause()
            else:
                self.mediaPlayer.play()
    
        def mediaStateChanged(self, state):
            if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
                self.playButton.setIcon(
                        self.style().standardIcon(QStyle.SP_MediaPause))
            else:
                self.playButton.setIcon(
                        self.style().standardIcon(QStyle.SP_MediaPlay))
    
        def positionChanged(self, position):
            self.positionSlider.setValue(position)
    
        def durationChanged(self, duration):
            self.positionSlider.setRange(0, duration)
    
        def setPosition(self, position):
            self.mediaPlayer.setPosition(position)
    
        def handleError(self):
            self.playButton.setEnabled(False)
            self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())
    
    
    if __name__ == '__main__':
    
        import sys
    
        app = QApplication(sys.argv)
    
        player = VideoPlayer()
        player.resize(320, 240)
        player.show()
    
        sys.exit(app.exec_())
    

    例子2

    from PyQt5.QtGui import QIcon, QFont
    from PyQt5.QtCore import QDir, Qt, QUrl, QSize
    from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
    from PyQt5.QtMultimediaWidgets import QVideoWidget
    from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel, 
            QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QStatusBar)
    
    class VideoPlayer(QWidget):
    
        def __init__(self, parent=None):
            super(VideoPlayer, self).__init__(parent)
    
            self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
    
            btnSize = QSize(16, 16)
            videoWidget = QVideoWidget()
    
            openButton = QPushButton("Open Video")   
            openButton.setToolTip("Open Video File")
            openButton.setStatusTip("Open Video File")
            openButton.setFixedHeight(24)
            openButton.setIconSize(btnSize)
            openButton.setFont(QFont("Noto Sans", 8))
            openButton.setIcon(QIcon.fromTheme("document-open", QIcon("D:/_Qt/img/open.png")))
            openButton.clicked.connect(self.abrir)
    
            self.playButton = QPushButton()
            self.playButton.setEnabled(False)
            self.playButton.setFixedHeight(24)
            self.playButton.setIconSize(btnSize)
            self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
            self.playButton.clicked.connect(self.play)
    
            self.positionSlider = QSlider(Qt.Horizontal)
            self.positionSlider.setRange(0, 0)
            self.positionSlider.sliderMoved.connect(self.setPosition)
    
            self.statusBar = QStatusBar()
            self.statusBar.setFont(QFont("Noto Sans", 7))
            self.statusBar.setFixedHeight(14)
    
            controlLayout = QHBoxLayout()
            controlLayout.setContentsMargins(0, 0, 0, 0)
            controlLayout.addWidget(openButton)
            controlLayout.addWidget(self.playButton)
            controlLayout.addWidget(self.positionSlider)
    
            layout = QVBoxLayout()
            layout.addWidget(videoWidget)
            layout.addLayout(controlLayout)
            layout.addWidget(self.statusBar)
    
            self.setLayout(layout)
    
            self.mediaPlayer.setVideoOutput(videoWidget)
            self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
            self.mediaPlayer.positionChanged.connect(self.positionChanged)
            self.mediaPlayer.durationChanged.connect(self.durationChanged)
            self.mediaPlayer.error.connect(self.handleError)
            self.statusBar.showMessage("Ready")
    
        def abrir(self):
            fileName, _ = QFileDialog.getOpenFileName(self, "Selecciona los mediose",
                    ".", "Video Files (*.mp4 *.flv *.ts *.mts *.avi)")
    
            if fileName != '':
                self.mediaPlayer.setMedia(
                        QMediaContent(QUrl.fromLocalFile(fileName)))
                self.playButton.setEnabled(True)
                self.statusBar.showMessage(fileName)
                self.play()
    
        def play(self):
            if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
                self.mediaPlayer.pause()
            else:
                self.mediaPlayer.play()
    
        def mediaStateChanged(self, state):
            if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
                self.playButton.setIcon(
                        self.style().standardIcon(QStyle.SP_MediaPause))
            else:
                self.playButton.setIcon(
                        self.style().standardIcon(QStyle.SP_MediaPlay))
    
        def positionChanged(self, position):
            self.positionSlider.setValue(position)
    
        def durationChanged(self, duration):
            self.positionSlider.setRange(0, duration)
    
        def setPosition(self, position):
            self.mediaPlayer.setPosition(position)
    
        def handleError(self):
            self.playButton.setEnabled(False)
            self.statusBar.showMessage("Error: " + self.mediaPlayer.errorString())
    
    if __name__ == '__main__':
        import sys
        app = QApplication(sys.argv)
        player = VideoPlayer()
        player.setWindowTitle("Player")
        player.resize(600, 400)
        player.show()
        sys.exit(app.exec_())
    
    爱技术的程序员,在职上岸非全研究生;技术兴趣广泛:架构、java、python、nodejs、性能优化、工具、大数据、AI等技术分享!
  • 相关阅读:
    极光推送
    ModelAndView跳转页面的时候,显示了页面的源码问题
    关于字符串比较时候出现的空指针问题的坑
    JAVA的extends用法
    C# 平面文件批量导数据到DB(二)
    C# 平面文件批量导数据到DB(一)
    C#操作文件和文件夹的类介绍
    C# 实现监控文件夹和里面文件的变化
    SQL Server 2012 Throw关键字
    SQL SERVER EXCEPT 和 INTERSECT
  • 原文地址:https://www.cnblogs.com/qingmiaokeji/p/14758340.html
Copyright © 2011-2022 走看看