Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [PyQt] Can't show PyQt5 mediaPlayer in window
#1
I'm trying to add mediaPlayer to my window for few days but I just can't show it. Qt Designer don't have a option for adding mediaPlayer and I can't add it manually. Any help? Thanks.

Last try:

from PyQt5 import QtCore, QtGui, QtWidgets
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 Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(680, 524)
        MainWindow.setStyleSheet("background-image: url(program_background3);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox.setGeometry(QtCore.QRect(450, 420, 62, 22))
        self.doubleSpinBox.setObjectName("doubleSpinBox")
        self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_2.setGeometry(QtCore.QRect(110, 420, 62, 22))
        self.doubleSpinBox_2.setObjectName("doubleSpinBox_2")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(490, 270, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(0, 390, 681, 20))
        self.widget.setStyleSheet("background: #222;")
        self.widget.setObjectName("widget")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(210, 440, 191, 41))
        self.pushButton_2.setStyleSheet("background: #3D94F6;\n"
"background-image: -webkit-linear-gradient(top, #3D94F6, #1E62D0);\n"
"background-image: -moz-linear-gradient(top, #3D94F6, #1E62D0);\n"
"background-image: -ms-linear-gradient(top, #3D94F6, #1E62D0);\n"
"background-image: -o-linear-gradient(top, #3D94F6, #1E62D0);\n"
"background-image: linear-gradient(to bottom, #3D94F6, #1E62D0);\n"
"-webkit-border-radius: 20px;\n"
"-moz-border-radius: 20px;\n"
"border-radius: 20px;\n"
"border-background: transparent;\n"
"   color: #FFFFFF;\n"
"   font-family: Open Sans;\n"
"   font-size: 20px;\n"
"   font-weight: 100;\n"
"   padding: 40px;\n"
"   box-shadow: 1px 1px 20px 0px #000000;\n"
"   -webkit-box-shadow: 1px 1px 20px 0px #000000;\n"
"   -moz-box-shadow: 1px 1px 20px 0px #000000;\n"
"   text-shadow: 1px 1px 20px #000000;\n"
"   border: solid #337FED 1px;\n"
"   text-decoration: none;\n"
"   display: inline-block;\n"
"   cursor: pointer;")
        self.pushButton_2.setObjectName("pushButton_2")
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)

        self.videoWidget = QVideoWidget()

        self.openButton = QPushButton("Open...")
        self.openButton.clicked.connect(self.openFile)

        self.playButton = QPushButton()
        self.playButton.setEnabled(False)
        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)

        self.controlLayout = QHBoxLayout()
        self.controlLayout.setGeometry(QtCore.QRect(110, 110, 421, 221))
        #self.controlLayout.setContentsMargins(0, 0, 0, 0)
        self.controlLayout.addWidget(self.openButton)
        self.controlLayout.addWidget(self.playButton)
        self.controlLayout.addWidget(self.positionSlider)

        self.layout = QVBoxLayout()
        self.layout.addWidget(self.videoWidget)
        self.layout.addLayout(self.controlLayout)
        self.layout.addWidget(self.errorLabel)

        MainWindow.setLayout(self.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)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 680, 21))
        self.menubar.setObjectName("menubar")
        self.menuAdd_video = QtWidgets.QMenu(self.menubar)
        self.menuAdd_video.setObjectName("menuAdd_video")
        self.menuAdd_sound = QtWidgets.QMenu(self.menubar)
        self.menuAdd_sound.setObjectName("menuAdd_sound")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuAdd_video.menuAction())
        self.menubar.addAction(self.menuAdd_sound.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Save As"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
        self.menuAdd_video.setTitle(_translate("MainWindow", "Add video"))
        self.menuAdd_sound.setTitle(_translate("MainWindow", "Add sound"))
    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())

from PyQt5.QtMultimediaWidgets import QVideoWidget

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Quote
#2
I've removed all stylesheet values that don't exist and changed your menu to toolbar buttons with actions.

from PyQt5 import QtCore, QtGui, QtWidgets
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, QMainWindow)
 
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(680, 542)
        MainWindow.setStyleSheet("background-image: url(program_background3);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.doubleSpinBox = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox.setGeometry(QtCore.QRect(450, 450, 62, 22))
        self.doubleSpinBox.setObjectName("doubleSpinBox")
        self.doubleSpinBox_2 = QtWidgets.QDoubleSpinBox(self.centralwidget)
        self.doubleSpinBox_2.setGeometry(QtCore.QRect(110, 450, 62, 22))
        self.doubleSpinBox_2.setObjectName("doubleSpinBox_2")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(540, 450, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(0, 0, 681,420))
        self.widget.setStyleSheet("background: #222;")
        self.widget.setObjectName("widget")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(210, 440, 191, 44))
        self.pushButton_2.setText("Push Button")
        self.pushButton_2.clicked.connect(self.clickBtn)
        self.pushButton_2.setStyleSheet("background: #3D94F6;"
"background-image: linear-gradient(top, #3D94F6, #1E62D0);"
"border-radius: 20px;"
"color: #FFFFFF;"
"font-family: Open Sans;"
"font-size: 20px;"
"font-weight: normal;"
"padding: 10px;"
"border: solid #337FED 1px;"
"text-decoration: none;")

        self.pushButton_2.setObjectName("pushButton_2")
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
 
        self.videoWidget = QVideoWidget()
        self.videoWidget.setStyleSheet("background: #3D94F6;")
        self.videoWidget.setGeometry(QtCore.QRect(10, 10, 420, 350))
 
        self.openButton = QPushButton("Open...")
        self.openButton.clicked.connect(self.openFile)
 
        self.playButton = QPushButton()
        self.playButton.setEnabled(False)
        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)
 
        self.controlLayout = QHBoxLayout()
        self.controlLayout.setGeometry(QtCore.QRect(10, 6, 421, 30))
        #self.controlLayout.setContentsMargins(0, 0, 0, 0)
        self.controlLayout.addWidget(self.openButton)
        self.controlLayout.addWidget(self.playButton)
        self.controlLayout.addWidget(self.positionSlider)
 
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.videoWidget)
        self.layout.addLayout(self.controlLayout)
        self.layout.addWidget(self.errorLabel)
        
 
        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)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QToolBar(MainWindow)
#        self.menubar.setGeometry(QtCore.QRect(0, 0, 680, 21))
        self.menubar.setObjectName("menubar")

        self.menuAdd_video = QtWidgets.QAction("add video", triggered = self.add_video)
        self.menuAdd_video.setObjectName("menuAdd_video")
        self.menuAdd_sound = QtWidgets.QAction("add sound", triggered = self.add_sound)
        self.menuAdd_sound.setObjectName("menuAdd_sound")

        MainWindow.addToolBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.menubar.addAction(self.menuAdd_video)
        self.menubar.addAction(self.menuAdd_sound)
 
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def clickBtn(self):
        self.statusbar.showMessage("Button clicked", 0)

    def add_video(self):
        self.statusbar.showMessage("add video", 0)

    def add_sound(self):
        self.statusbar.showMessage("add sound", 0)

    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())
 
from PyQt5.QtMultimediaWidgets import QVideoWidget
 
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Qt Designer uses a lot of setGeometry, I use setGeometry for the main Window only.

If you want to learn it better don't use Qt Designer.

here's a simple example without using designer

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()
        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"))
        openButton.clicked.connect(self.openFile)

        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 openFile(self):
        fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
                QDir.homePath() + "/Videos", "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.setGeometry(0, 0, 600, 400)
    player.show()
    sys.exit(app.exec_())
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 277 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  How to show graph as a slideshow in PyQt5 using matplotlib binsha 0 770 Mar-08-2019, 03:58 AM
Last Post: binsha
  [PYQT5] Crashing when using .hide() and .show() aking76 0 1,257 Sep-18-2018, 02:09 PM
Last Post: aking76
  update a variable in parent window after closing its toplevel window gray 5 2,676 Mar-20-2017, 10:35 PM
Last Post: Larz60+
  Show second window using classes iFunKtion 2 1,522 Feb-10-2017, 12:50 PM
Last Post: iFunKtion
  Show GPG keys in pyqt5 window Fred Barclay 1 1,849 Oct-24-2016, 07:09 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)