Always display frame of QVideoWidget
-
I have a widget called ThumbnailVideoWidget which plays a short video clip with included play button and position slider widgets. I am using PySide2-5.9.0a1.
My issues are:
-
Initially no frames are shown in the QVideoWidget, its just a transparent background. Is there a way to initialize a QVideoWidget/QMediaPlayer with a video and display the first frame automatically?
-
When I click play and then pause, everything plays properly but when I move my mouse off of the play button, the video goes blank/transparent again. The expected behavior would be to stop and keep on the current frame.
Source code of widget:
class ThumbnailVideoWidget(QtWidgets.QWidget): def __init__(self, path, parent=None): super(ThumbnailVideoWidget, self).__init__(parent) self.setup_ui() self.connect_signals() self.set_media(path) def setup_ui(self): self.media_player = QtMultimedia.QMediaPlayer(self, QtMultimedia.QMediaPlayer.VideoSurface) self.w_video = QtMultimediaWidgets.QVideoWidget() self.b_play = QtWidgets.QPushButton() self.b_play.setEnabled(False) self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay)) self.position_slider = QtWidgets.QSlider(QtCore.Qt.Horizontal) self.position_slider.setRange(0, 0) self.media_player.setVideoOutput(self.w_video) self.lyt_main = QtWidgets.QVBoxLayout() self.lyt_main.addWidget(self.w_video) self.lyt_main.addWidget(self.b_play) self.lyt_main.addWidget(self.position_slider) self.setLayout(self.lyt_main) def connect_signals(self): self.position_slider.sliderMoved.connect(self.set_position) self.b_play.clicked.connect(self.play) self.media_player.stateChanged.connect(self.media_state_changed) self.media_player.positionChanged.connect(self.position_changed) self.media_player.durationChanged.connect(self.duration_changed) def set_media(self, path): self.media_player.setMedia(QtCore.QUrl.fromLocalFile(path)) self.b_play.setEnabled(True) def play(self): if self.media_player.state() == QtMultimedia.QMediaPlayer.PlayingState: self.media_player.pause() else: self.media_player.play() def set_position(self, position): self.media_player.setPosition(position) def media_state_changed(self, state): if self.media_player.state() == QtMultimedia.QMediaPlayer.PlayingState: self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPause)) else: self.b_play.setIcon(self.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay)) def position_changed(self, position): self.position_slider.setValue(position) def duration_changed(self, duration): self.position_slider.setRange(0, duration) interval = int(float(duration) / 10.0) self.media_player.setNotifyInterval(interval)
-
-
Hi,
This version of PySide2 being pretty old, I would recommend updating before digging further.
Also, what OS are you running that on ?
How did you install PySide2 ? -
You can install it using pip, no need to build it from scratch.
-
Ah yes, it seems that there's indeed no Py27 per-built package for Windows.
-
Great !
The please mark the thread as solved using the "Topic Tools" button or the three doted menu beside the answer you deem correct so that other forum users may know a solution has been found :-)