I have a media with mp4 format. I am trying to put this video in self.ui.videoPlayerWidget
widget. I can hear the voice of video but video not showing in the window.
I created QVBoxLayout and add videoWidget in this layout with this command:
vbox = QVBoxLayout()
vbox.setContentsMargins(0,0,0,0)
vbox.addWidget(self.videoWidget)
After the VideoOutput line I add this layout to my video vidget from the ui:
self.ui.videoPlayerWidget.setLayout(vbox) #video not showing in there
But it does not work.
When I use self.setCentralWidget(self.videoWidget)
instead of self.mediaPlayer.setVideoOutput(self.videoWidget) I can see video, but I dont want this.
Where is my fault ?
Source Code main.py
from PyQt5 import QtWidgets, QtMultimediaWidgets, QtMultimedia, QtCore, QtGui, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QLineEdit, QComboBox,QVBoxLayout,QHBoxLayout
from PyQt5.QtGui import QTransform
import sys
from ScanCodeMedia import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self,parent=None):
QMainWindow.__init__(self,parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
movie_file = QtCore.QUrl.fromLocalFile('./galaxy_background.mp4')
vid_media = QtMultimedia.QMediaContent(movie_file)
# create video widget
self.videoWidget = QtMultimediaWidgets.QVideoWidget()
# self.videoWidget.setGeometry(0, 0, 1920, 1080)
vbox = QVBoxLayout()
vbox.setContentsMargins(0,0,0,0)
vbox.addWidget(self.videoWidget)
# create media player object (video widget goes in media player)
self.mediaPlayer = QtMultimedia.QMediaPlayer(None,
QtMultimedia.QMediaPlayer.VideoSurface)
self.mediaPlayer.setVideoOutput(self.videoWidget)
self.ui.videoPlayerWidget.setLayout(vbox) #video not showing in there
# playlist
self.playlist = QtMultimedia.QMediaPlaylist()
self.playlist.setCurrentIndex(0)
self.playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop)
self.playlist.addMedia(vid_media)
# add content to media player
self.mediaPlayer.setPlaylist(self.playlist)
self.mediaPlayer.play()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
And Ui_Window file:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'media_player_test.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
MainWindow.setMinimumSize(QtCore.QSize(1920, 1080))
MainWindow.setMaximumSize(QtCore.QSize(1920, 1080))
MainWindow.setStyleSheet("\n"
"\n"
"QPushButton{\n"
"background-color: transparent;\n"
"}")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(1920, 1080))
self.centralwidget.setMaximumSize(QtCore.QSize(1920, 1080))
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.videoPlayerWidget = QtWidgets.QWidget(self.centralwidget)
self.videoPlayerWidget.setMinimumSize(QtCore.QSize(1920, 1080))
self.videoPlayerWidget.setMaximumSize(QtCore.QSize(1920, 1080))
self.videoPlayerWidget.setObjectName("videoPlayerWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.videoPlayerWidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout.addWidget(self.videoPlayerWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))