Search code examples
pythonpyqtmedia-player

Why doesn't my wav music file play in Qsound?


This is my code :

from PyQt5.QtMultimedia import QSound
QSound("1.wav").play()

It doesn't play the music. The file is 37 MB.


Solution

  • You have to create a QXXXAplication to create the necessary loop to reproduce the sound, on the other hand the correct thing is to create the object and then make play():

    from PyQt5.QtCore import QCoreApplication
    from PyQt5.QtMultimedia import QSound
    import sys
    
    if __name__ == '__main__':
        app = QCoreApplication(sys.argv)
        sound = QSound("1.wav")
        sound.play()
        sys.exit(app.exec_())
    

    Or if you just want to play and not modify any feature, use the static play() method:

    from PyQt5.QtCore import QCoreApplication
    from PyQt5.QtMultimedia import QSound
    import sys
    
    if __name__ == '__main__':
        app = QCoreApplication(sys.argv)
        QSound.play("1.wav")
        sys.exit(app.exec_())
    

    Obs:

    If you want to use it inside a GUI you must change QCoreApplication to QApplication. I'm also assuming that the .wav file is next to the .py file.