简体   繁体   English

PyQt6 QMediaPlayer 和 QAudioOutput 未按预期运行

[英]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.我一直在使用 QAudioOutput 和 QMediaPlayer 迁移到 PyQt6 时遇到问题,其中 QMediaPlayer object 似乎不适用于我制作的任何 QAudioOutput。 If I set a QAudioOutput object the video will fail to render and the event loop gets sluggish like buggy things are happening.如果我设置 QAudioOutput object 视频将无法呈现并且事件循环变得缓慢,就像发生错误一样。 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.此外,当使用 QMediaPlayer.setAudioOutput 时,QMediaPlayer 似乎不会增加 QAudioOutput 对象的引用计数器,因为除非我自己保留对 object 的引用,否则它会被清除。

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.对我来说,上面的代码将运行并播放视频文件(路径的第一个参数),但“player.setAudioOutput”被注释掉了。 If it is uncommented then the player will fail.如果未注释,则播放器将失败。 I've tried manually setting the QAudioDevice and PyQt (6.2.3, 6.2.2).我试过手动设置 QAudioDevice 和 PyQt (6.2.3, 6.2.2)。 Despite messing around for quite a while I can't get anything to work.尽管闲逛了很长一段时间,但我什么也做不了。 Any ideas?有任何想法吗?

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.虽然不是这个问题的解决方案,但我确定问题出在 windows 上的 vorbis 音频编解码器。由于 Qt 放弃了 DirectShow 并且仅支持 WMF,这导致我的计算机出现问题。 Unfortunately, I have not been able to get Qt to cooperate with any attempts to install codecs.不幸的是,我无法获得 Qt 来配合任何安装编解码器的尝试。 Not 3rd party codecs or the "Web Media Extensions" from ms store.不是第 3 方编解码器或来自 ms store 的“Web Media Extensions”。 Below is some code that appears to prove that the vorbis codec is the issue (along with only files needing that codec breaking Qt):下面是一些代码,似乎可以证明 vorbis 编解码器是问题所在(以及仅需要该编解码器破坏 Qt 的文件):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM