0

I want to add a Video player inside this box.

Screen shot of the UI:
Screen shot of the UI

I have designed the UI in Qt Designer and this box is a Qwidget named "video". I don't know how to integrate the Video player inside of this box. I am aiming to upload the fetch the file directory from the button "Upload Video" and then play the video from the directory in the Video Player that will be in the box. I would appreciate the help.

Here is the GUI code for the widget that has been inserted.

self.video = QWidget(self.Home_page)
self.video.setObjectName(u"video")
self.video.setStyleSheet(u"border:1px solid white\n""")

self.verticalLayout_6.addWidget(self.video)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

You can use a QFileDialog to let the user choose the file he wants. It will provide you the filesystem path to the file. Then, you can use a QMediaPlayer to load the file using its path.

Edit : Example (I used PySide but it should work if you replace with PyQt) :

from PySide2 import QtCore, QtWidgets, QtMultimedia, QtMultimediaWidgets


class VerySimpleMediaPlayer(QtWidgets.QWidget):
    def __init__(self, parent):
        super().__init__(parent)
        self.open_file_button = QtWidgets.QPushButton("Open file")
        self.open_file_button.clicked.connect(self.open_file)

        self.media_player = QtMultimedia.QMediaPlayer(self)
        self.media_widget = QtMultimediaWidgets.QVideoWidget(self)
        self.media_player.setVideoOutput(self.media_widget)
        self.media_widget.show()

        layout = QtWidgets.QVBoxLayout()
        layout.addWidget(self.open_file_button)
        layout.addWidget(self.media_widget)
        self.setLayout(layout)

    def open_file(self):
        filepath, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Choose video file to load")
        self.media_player.setMedia(QtCore.QUrl.fromLocalFile(filepath))
        self.media_player.setVolume(20)  # not too loud
        self.media_player.play()


if __name__ == "__main__":
    app = QtWidgets.QApplication([])

    main_window = QtWidgets.QMainWindow()

    example_widget = VerySimpleMediaPlayer(main_window)
    main_window.setCentralWidget(example_widget)

    main_window.setVisible(True)
    app.exec_()

It worked for listening to a MP3 file, but did not work for a MP4 file, maybe because of codecs. Try it yourself.

I used these docs :

Lenormju
  • 4,078
  • 2
  • 8
  • 22
  • Thanks for your answer, but I am able to do that. I don't know how to integrate Qmediaplayer inside the Qwidget i placed in my GUI. if you could help with that, please. – Osama Jilani May 27 '21 at 13:14