1 2############################################################################# 3## 4## Copyright (C) 2017 The Qt Company Ltd. 5## Contact: http://www.qt.io/licensing/ 6## 7## This file is part of the Qt for Python examples of the Qt Toolkit. 8## 9## $QT_BEGIN_LICENSE:BSD$ 10## You may use this file under the terms of the BSD license as follows: 11## 12## "Redistribution and use in source and binary forms, with or without 13## modification, are permitted provided that the following conditions are 14## met: 15## * Redistributions of source code must retain the above copyright 16## notice, this list of conditions and the following disclaimer. 17## * Redistributions in binary form must reproduce the above copyright 18## notice, this list of conditions and the following disclaimer in 19## the documentation and/or other materials provided with the 20## distribution. 21## * Neither the name of The Qt Company Ltd nor the names of its 22## contributors may be used to endorse or promote products derived 23## from this software without specific prior written permission. 24## 25## 26## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 37## 38## $QT_END_LICENSE$ 39## 40############################################################################# 41 42"""PySide2 Multimedia player example""" 43 44import sys 45from PySide2.QtCore import QStandardPaths, Qt 46from PySide2.QtGui import QIcon, QKeySequence 47from PySide2.QtWidgets import (QAction, QApplication, QDialog, QFileDialog, 48 QMainWindow, QSlider, QStyle, QToolBar) 49from PySide2.QtMultimedia import QMediaPlayer, QMediaPlaylist 50from PySide2.QtMultimediaWidgets import QVideoWidget 51 52class MainWindow(QMainWindow): 53 54 def __init__(self): 55 super(MainWindow, self).__init__() 56 57 self.playlist = QMediaPlaylist() 58 self.player = QMediaPlayer() 59 60 toolBar = QToolBar() 61 self.addToolBar(toolBar) 62 63 fileMenu = self.menuBar().addMenu("&File") 64 openAction = QAction(QIcon.fromTheme("document-open"), 65 "&Open...", self, shortcut=QKeySequence.Open, 66 triggered=self.open) 67 fileMenu.addAction(openAction) 68 exitAction = QAction(QIcon.fromTheme("application-exit"), "E&xit", 69 self, shortcut="Ctrl+Q", triggered=self.close) 70 fileMenu.addAction(exitAction) 71 72 playMenu = self.menuBar().addMenu("&Play") 73 playIcon = self.style().standardIcon(QStyle.SP_MediaPlay) 74 self.playAction = toolBar.addAction(playIcon, "Play") 75 self.playAction.triggered.connect(self.player.play) 76 playMenu.addAction(self.playAction) 77 78 previousIcon = self.style().standardIcon(QStyle.SP_MediaSkipBackward) 79 self.previousAction = toolBar.addAction(previousIcon, "Previous") 80 self.previousAction.triggered.connect(self.previousClicked) 81 playMenu.addAction(self.previousAction) 82 83 pauseIcon = self.style().standardIcon(QStyle.SP_MediaPause) 84 self.pauseAction = toolBar.addAction(pauseIcon, "Pause") 85 self.pauseAction.triggered.connect(self.player.pause) 86 playMenu.addAction(self.pauseAction) 87 88 nextIcon = self.style().standardIcon(QStyle.SP_MediaSkipForward) 89 self.nextAction = toolBar.addAction(nextIcon, "Next") 90 self.nextAction.triggered.connect(self.playlist.next) 91 playMenu.addAction(self.nextAction) 92 93 stopIcon = self.style().standardIcon(QStyle.SP_MediaStop) 94 self.stopAction = toolBar.addAction(stopIcon, "Stop") 95 self.stopAction.triggered.connect(self.player.stop) 96 playMenu.addAction(self.stopAction) 97 98 self.volumeSlider = QSlider() 99 self.volumeSlider.setOrientation(Qt.Horizontal) 100 self.volumeSlider.setMinimum(0) 101 self.volumeSlider.setMaximum(100) 102 self.volumeSlider.setFixedWidth(app.desktop().availableGeometry(self).width() / 10) 103 self.volumeSlider.setValue(self.player.volume()) 104 self.volumeSlider.setTickInterval(10) 105 self.volumeSlider.setTickPosition(QSlider.TicksBelow) 106 self.volumeSlider.setToolTip("Volume") 107 self.volumeSlider.valueChanged.connect(self.player.setVolume) 108 toolBar.addWidget(self.volumeSlider) 109 110 aboutMenu = self.menuBar().addMenu("&About") 111 aboutQtAct = QAction("About &Qt", self, triggered=qApp.aboutQt) 112 aboutMenu.addAction(aboutQtAct) 113 114 self.videoWidget = QVideoWidget() 115 self.setCentralWidget(self.videoWidget) 116 self.player.setPlaylist(self.playlist) 117 self.player.stateChanged.connect(self.updateButtons) 118 self.player.setVideoOutput(self.videoWidget) 119 120 self.updateButtons(self.player.state()) 121 122 def open(self): 123 fileDialog = QFileDialog(self) 124 supportedMimeTypes = QMediaPlayer.supportedMimeTypes() 125 if not supportedMimeTypes: 126 supportedMimeTypes.append("video/x-msvideo") # AVI 127 fileDialog.setMimeTypeFilters(supportedMimeTypes) 128 moviesLocation = QStandardPaths.writableLocation(QStandardPaths.MoviesLocation) 129 fileDialog.setDirectory(moviesLocation) 130 if fileDialog.exec_() == QDialog.Accepted: 131 self.playlist.addMedia(fileDialog.selectedUrls()[0]) 132 self.player.play() 133 134 def previousClicked(self): 135 # Go to previous track if we are within the first 5 seconds of playback 136 # Otherwise, seek to the beginning. 137 if self.player.position() <= 5000: 138 self.playlist.previous() 139 else: 140 player.setPosition(0) 141 142 def updateButtons(self, state): 143 mediaCount = self.playlist.mediaCount() 144 self.playAction.setEnabled(mediaCount > 0 145 and state != QMediaPlayer.PlayingState) 146 self.pauseAction.setEnabled(state == QMediaPlayer.PlayingState) 147 self.stopAction.setEnabled(state != QMediaPlayer.StoppedState) 148 self.previousAction.setEnabled(self.player.position() > 0) 149 self.nextAction.setEnabled(mediaCount > 1) 150 151if __name__ == '__main__': 152 app = QApplication(sys.argv) 153 mainWin = MainWindow() 154 availableGeometry = app.desktop().availableGeometry(mainWin) 155 mainWin.resize(availableGeometry.width() / 3, availableGeometry.height() / 2) 156 mainWin.show() 157 sys.exit(app.exec_()) 158