2021-04-15 15:16:38 -05:00
|
|
|
#!/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$
|
|
|
|
##
|
|
|
|
#############################################################################
|
|
|
|
|
2021-04-17 10:51:56 -05:00
|
|
|
from PyQt5 import QtWidgets, QtCore
|
|
|
|
from PyQt5.QtCore import QDir, Qt, QUrl, QSize
|
|
|
|
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QMediaPlaylist
|
|
|
|
from PyQt5.QtMultimediaWidgets import QVideoWidget
|
|
|
|
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
|
2021-04-15 15:16:38 -05:00
|
|
|
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget, QDesktopWidget)
|
2021-04-17 10:51:56 -05:00
|
|
|
from PyQt5.QtGui import QDesktopServices
|
2021-04-15 15:16:38 -05:00
|
|
|
|
|
|
|
|
|
|
|
class VideoPlayer(QWidget):
|
|
|
|
|
|
|
|
def __init__(self, url, parent=None):
|
|
|
|
super(VideoPlayer, self).__init__(parent)
|
|
|
|
|
|
|
|
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Preferred)
|
|
|
|
|
|
|
|
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
|
|
|
|
|
|
|
|
self.videoWidget = LinkPlayer(url)
|
|
|
|
self.videoWidget.setAspectRatioMode(Qt.KeepAspectRatio)
|
|
|
|
|
|
|
|
self.playlist = QMediaPlaylist()
|
|
|
|
self.playlist.setPlaybackMode(QMediaPlaylist.Loop)
|
|
|
|
|
|
|
|
self.mediaPlayer.setPlaylist(self.playlist)
|
|
|
|
|
|
|
|
self.playButton = QPushButton()
|
|
|
|
self.playButton.setEnabled(False)
|
|
|
|
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
|
|
|
|
self.playButton.clicked.connect(self.play)
|
|
|
|
|
|
|
|
self.positionSlider = ClickSlider()
|
|
|
|
self.positionSlider.setOrientation(Qt.Horizontal)
|
|
|
|
self.positionSlider.setRange(0, 0)
|
|
|
|
self.positionSlider.sliderMoved.connect(self.setPosition)
|
|
|
|
|
|
|
|
#self.volumeSlider = QSlider(Qt.Horizontal)
|
|
|
|
#self.volumeSlider.setRange(0, 100)
|
|
|
|
#self.volumeSlider.setSliderPosition(100)
|
|
|
|
#self.volumeSlider.sliderMoved.connect(self.mediaPlayer.setVolume)
|
|
|
|
|
|
|
|
self.volumeButton = QPushButton()
|
|
|
|
self.volumeButton.setEnabled(False)
|
|
|
|
self.volumeButton.setIcon(self.style().standardIcon(QStyle.SP_MediaVolume))
|
|
|
|
self.volumeButton.clicked.connect(self.doMute)
|
|
|
|
|
|
|
|
self.errorLabel = QLabel()
|
|
|
|
self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
|
|
|
|
QSizePolicy.Maximum)
|
|
|
|
|
|
|
|
controlLayout = QHBoxLayout()
|
|
|
|
controlLayout.setContentsMargins(0, 0, 0, 0)
|
|
|
|
controlLayout.addWidget(self.playButton, 1)
|
|
|
|
controlLayout.addWidget(self.positionSlider, 10)
|
|
|
|
controlLayout.addWidget(self.volumeButton, 1)
|
|
|
|
#controlLayout.addWidget(self.volumeSlider, 1)
|
|
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
layout.addWidget(self.videoWidget)
|
|
|
|
layout.addLayout(controlLayout)
|
|
|
|
layout.addWidget(self.errorLabel)
|
|
|
|
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
self.mediaPlayer.setVideoOutput(self.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):
|
|
|
|
if fileName != '':
|
|
|
|
|
|
|
|
self.playlist.addMedia(QMediaContent(QUrl(fileName)))
|
|
|
|
self.playlist.next()
|
|
|
|
|
|
|
|
|
|
|
|
self.mediaPlayer.play()
|
|
|
|
self.mediaPlayer.pause()
|
|
|
|
#self.mediaPlayer.setMedia(
|
|
|
|
# QMediaContent(QUrl(fileName)))
|
|
|
|
#QMediaContent(QUrl.fromLocalFile(fileName)))
|
|
|
|
self.playButton.setEnabled(True)
|
|
|
|
self.volumeButton.setEnabled(True)
|
|
|
|
#self.mediaPlayer.playlist().setPlaybackMode(QMediaPlaylist.Loop)
|
|
|
|
|
|
|
|
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 doMute(self, checked):
|
|
|
|
self.mediaPlayer.setMuted(not self.mediaPlayer.isMuted())
|
|
|
|
if self.mediaPlayer.isMuted():
|
|
|
|
self.volumeButton.setIcon(self.style().standardIcon(QStyle.SP_MediaVolumeMuted))
|
|
|
|
else:
|
|
|
|
self.volumeButton.setIcon(self.style().standardIcon(QStyle.SP_MediaVolume))
|
|
|
|
|
|
|
|
def handleError(self):
|
|
|
|
self.playButton.setEnabled(False)
|
|
|
|
self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())
|
|
|
|
|
|
|
|
def sizeHint(self):
|
|
|
|
return QSize(250, round(QDesktopWidget().screenGeometry(-1).height() * 0.6))
|
|
|
|
|
|
|
|
class ClickSlider(QtWidgets.QSlider):
|
|
|
|
def mousePressEvent(self, event):
|
|
|
|
super(ClickSlider, self).mousePressEvent(event)
|
|
|
|
if event.button() == QtCore.Qt.LeftButton:
|
|
|
|
val = self.pixelPosToRangeValue(event.pos())
|
|
|
|
self.setValue(val)
|
|
|
|
|
|
|
|
def pixelPosToRangeValue(self, pos):
|
|
|
|
opt = QtWidgets.QStyleOptionSlider()
|
|
|
|
self.initStyleOption(opt)
|
|
|
|
gr = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt, QtWidgets.QStyle.SC_SliderGroove, self)
|
|
|
|
sr = self.style().subControlRect(QtWidgets.QStyle.CC_Slider, opt, QtWidgets.QStyle.SC_SliderHandle, self)
|
|
|
|
|
|
|
|
if self.orientation() == QtCore.Qt.Horizontal:
|
|
|
|
sliderLength = sr.width()
|
|
|
|
sliderMin = gr.x()
|
|
|
|
sliderMax = gr.right() - sliderLength + 1
|
|
|
|
else:
|
|
|
|
sliderLength = sr.height()
|
|
|
|
sliderMin = gr.y()
|
|
|
|
sliderMax = gr.bottom() - sliderLength + 1;
|
|
|
|
pr = pos - sr.center() + sr.topLeft()
|
|
|
|
p = pr.x() if self.orientation() == QtCore.Qt.Horizontal else pr.y()
|
|
|
|
return QtWidgets.QStyle.sliderValueFromPosition(self.minimum(), self.maximum(), p - sliderMin,
|
|
|
|
sliderMax - sliderMin, opt.upsideDown)
|
|
|
|
|
|
|
|
class LinkPlayer(QVideoWidget):
|
|
|
|
def __init__(self, url, *args, **kwargs):
|
|
|
|
super(LinkPlayer, self).__init__(*args, **kwargs)
|
|
|
|
self.url = QUrl(url)
|
|
|
|
super().setCursor(Qt.PointingHandCursor)
|
|
|
|
def mouseReleaseEvent(self, event):
|
|
|
|
bttn = event.button()
|
|
|
|
modif = event.modifiers()
|
|
|
|
if modif == Qt.NoModifier and bttn == Qt.LeftButton:
|
|
|
|
QDesktopServices.openUrl(self.url)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
|
|
|
player = VideoPlayer()
|
|
|
|
player.resize(320, 240)
|
|
|
|
player.show()
|
|
|
|
|
|
|
|
sys.exit(app.exec_())
|