Is it possible to make a video play instantly using QMediaPlayer?

64 views Asked by At

I am using a QMediaPlayer to output a video that contains audio to a QVideoWidget. When I use a video without audio it plays instantly, but if the video has audio, it takes a few seconds to play after calling player.play().

Is there a way to make a video with audio play instantly after calling play()?

I've tried following this answer but I'm getting Error: "failed to seek".

I am using Ubuntu 23.04, Python 3.11 and PyQt5.

Here's an example:

from PyQt5.QtWidgets import QApplication, QMainWindow, QWizard, QWizardPage, QPushButton, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtCore import QUrl
from PyQt5.QtMultimediaWidgets import QVideoWidget


class VideoPage(QWizardPage):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.player = QMediaPlayer(self)
        self.media_content = QMediaContent(QUrl.fromLocalFile('path/to/vid.mp4'))
        self.player.setMedia(self.media_content)
        self.video = QVideoWidget()
        self.player.setVideoOutput(self.video)
        
        play_button = QPushButton('Play Video')
        play_button.clicked.connect(self.play_video)
        layout.addWidget(self.video)
        layout.addWidget(play_button)

    def play_video(self):
        self.player.play()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    wizard = QWizard()
    video_page = VideoPage()
    wizard.addPage(video_page)
    wizard.setWindowTitle('Video Wizard')
    wizard.show()
    sys.exit(app.exec_())
0

There are 0 answers