Ryan Mcconnell
Ryan Mcconnell

Reputation: 86

PyQt6 QMediaPlayer and QAudioOutput not behaving as expected

I've been having an issue migrating to PyQt6 with QAudioOutput and QMediaPlayer where the QMediaPlayer object seems to not work with any QAudioOutput I make. If I set a QAudioOutput object the video will fail to render and the event loop gets sluggish like buggy things are happening. Also the QMediaPlayer does not seem to be incrementing the QAudioOutput object's reference counter when QMediaPlayer.setAudioOutput is used, because unless I keep a reference to the object myself it gets cleared.

Here is some demo code:

import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6.QtCore import QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget



class MainWin(QMainWindow):
    def __init__(self, file_path):
        super(MainWin, self).__init__()
        self.cent_wid = QVideoWidget()
        self.setCentralWidget(self.cent_wid)
        self.player = QMediaPlayer()
        self.audio_output = QAudioOutput()
        #self.player.setAudioOutput(self.audio_output)
        self.audio_output.setVolume(1.0)
        self.player.setVideoOutput(self.cent_wid)
        self.file_path = file_path

    def showEvent(self, a0) -> None:
        super(MainWin, self).showEvent(a0)
        self.player.setSource(QUrl.fromLocalFile(self.file_path))
        self.player.play()

if __name__ == '__main__':
    app = QApplication([])
    frm = MainWin(sys.argv[1])
    frm.show()
    app.exec()

For me, the above will run and play the video file (first argument for path), but the "player.setAudioOutput" is commented out. If it is uncommented then the player will fail. I've tried manually setting the QAudioDevice and PyQt (6.2.3, 6.2.2). Despite messing around for quite a while I can't get anything to work. Any ideas?

Upvotes: 0

Views: 2139

Answers (1)

Ryan Mcconnell
Ryan Mcconnell

Reputation: 86

Although not a solution to this problem I have determined that the issue is with the vorbis audio codec on windows. Since Qt dropped DirectShow and only supports WMF this caused an issue on my computer. Unfortunately, I have not been able to get Qt to cooperate with any attempts to install codecs. Not 3rd party codecs or the "Web Media Extensions" from ms store. Below is some code that appears to prove that the vorbis codec is the issue (along with only files needing that codec breaking Qt):

from PyQt6.QtMultimedia import QMediaFormat
mf = QMediaFormat()
for codec in mf.supportedAudioCodecs(QMediaFormat.ConversionMode.Decode):
    print(codec.name)

Upvotes: 1

Related Questions