简体   繁体   English

使用 PyQt6 播放声音

[英]Playing sounds with PyQt6

As the PyQt6 module has been released, I have started porting my code from PyQt5 to PyQt6.随着 PyQt6 模块的发布,我开始将我的代码从 PyQt5 移植到 PyQt6。

In PyQt, there was a module called phonon which was used to play sounds.在PyQt中,有一个叫做phonon的模块,是用来播放声音的。

In PyQt5, there was a module called QMediaPlayer which was then used to play sounds.在PyQt5中,有一个名为QMediaPlayer的模块,用于播放声音。

Now, how to play sound in PyQt6?现在,如何在 PyQt6 中播放声音?

There was a website which stated that the QMediaPlayer has not been ported yet and shall be done in the PyQt6 version PyQt6.2.有网站说QMediaPlayer还没有移植,应该在PyQt6版本的PyQt6.2上做。

The website is this - https://www.pythonguis.com/faq/pyqt-pyside6-missing-modules/网站是这个 - https://www.pythonguis.com/faq/pyqt-pyside6-missing-modules/

The website also states that the PyQt6.2 will be released in September 2021.该网站还表示 PyQt6.2 将于 2021 年 9 月发布。

Is the import renamed?导入重命名了吗?

It should be noted that:应当指出的是:

  • In Qt6 if you want to play a music file then you have 2 options:在 Qt6 中,如果你想播放音乐文件,那么你有两个选择:

    • QSoundEffect Q音效

      import sys from PyQt6.QtCore import QUrl from PyQt6.QtGui import QGuiApplication from PyQt6.QtMultimedia import QSoundEffect def main(): app = QGuiApplication(sys.argv) filename = "sound.wav" effect = QSoundEffect() effect.setSource(QUrl.fromLocalFile(filename)) # possible bug: QSoundEffect::Infinite cannot be used in setLoopCount effect.setLoopCount(-2) effect.play() sys.exit(app.exec()) if __name__ == "__main__": main()
    • QMediaPlayer. QMediaPlayer。

       import sys from PyQt6.QtCore import QUrl from PyQt6.QtGui import QGuiApplication from PyQt6.QtMultimedia import QAudioOutput, QMediaPlayer def main(): app = QGuiApplication(sys.argv) filename = "sound.mp3" player = QMediaPlayer() audio_output = QAudioOutput() player.setAudioOutput(audio_output) player.setSource(QUrl.fromLocalFile(filename)) audio_output.setVolume(50) player.play() sys.exit(app.exec()) if __name__ == "__main__": main()
  • The previous classes are available as of Qt 6.2 and at this moment there is no release available in pypi of PyQt6 6.2.0 but you can install it from the Riverbank Computing PyPI Server repositories (see here fore more information):之前的类从 Qt 6.2 开始可用,此时 PyQt6 6.2.0 的 pypi 中没有可用的版本,但您可以从 Riverbank Computing PyPI Server 存储库安装它(有关更多信息,请参见此处):

     python -m pip install --index-url https://riverbankcomputing.com/pypi/simple/ --pre --upgrade PyQt6

    Probably in a few days it will already be available in pypi可能几天后它就会在 pypi 中可用

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

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