0

As the PyQt6 module has been released, I have started porting my code from PyQt5 to PyQt6.

In PyQt, there was a module called phonon which was used to play sounds.

In PyQt5, there was a module called QMediaPlayer which was then used to play sounds.

Now, how to play sound in 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.

The website is this - https://www.pythonguis.com/faq/pyqt-pyside6-missing-modules/

The website also states that the PyQt6.2 will be released in September 2021.

Is the import renamed?

rpanai
  • 12,515
  • 2
  • 42
  • 64
BladeOfLightX
  • 504
  • 4
  • 17
  • According to the [Qt Documentation](https://doc.qt.io/qt-6/qtmultimedia-changes-qt6.html#removed-features), you should try using [`QSoundEffect`](https://doc.qt.io/qt-6/qsoundeffect.html). But as I don't have PyQt6 yet, I cannot confirm if it works. – mapf Oct 02 '21 at 10:10
  • Why dont you have PyQt6? PyQt6 is available on pypi. Moreover, the main QtMultiMedia is removed in PyQt6 – BladeOfLightX Oct 02 '21 at 10:28
  • What do you mean *why*? No it's not removed. I literally just linked to the documentation page. PyQt6.2 simply hasn't been released yet. – mapf Oct 02 '21 at 10:47
  • Yeah. However, like there was QtMediaPlayer in PyQt5 as an alternative for Phonon in PyQt4. Isnt there similar alternative in PyQt6? – BladeOfLightX Oct 02 '21 at 10:55
  • Well, yes. But it's just not part of it yet. – mapf Oct 02 '21 at 11:07
  • Oh I see. Well, then I guess I would have to wait for the release. – BladeOfLightX Oct 02 '21 at 11:09
  • Yeah. Since Qt6.2 has only been released [two days ago](https://wiki.qt.io/Qt_6.2_Release), I guess PyQt6.2 won't be released before next year. – mapf Oct 02 '21 at 11:14
  • Oh man! This isnt good. Alright then I guess I would have to just use some other modules :( – BladeOfLightX Oct 02 '21 at 12:36
  • 1
    @mapf You say: *I guess PyQt6.2 won't be released before next year*. where do you get that from? PyQt and PySide releases are generally in less than a week after Qt release. – eyllanesc Oct 02 '21 at 19:11
  • @AsianCat There is already a release available but it has not yet been uploaded to pypi but you can install it from the company page. – eyllanesc Oct 02 '21 at 19:12
  • 1
    @eyllanesc I was comparing the release dates from previous versions, but I just realized that I looked at the wrong dates. So it may just be a few days. – mapf Oct 02 '21 at 20:06

1 Answers1

7

It should be noted that:

  • In Qt6 if you want to play a music file then you have 2 options:

    • QSoundEffect

      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.

      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):

    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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241