I’m trying to play a music when a button is pressed.
The problem is I get the following error:
AttributeError: module 'PySide2' has no attribute 'QtMultimedia'
AttributeError: module 'PySide2.QtCore' has no attribute 'QtMultimedia'
Here is the code so far:
audio = PySide2.QtMultimedia.QSound('chain_frost_cast.wav')
I think you import it in wrong way!
you have to use it this way, notice the import statement:
from PySide2.QtMultimedia import QSound
audio = QSound('sample.wav')
Thanks for the response. It solves the attribute error.
I can execute the code but it doesn’t play the file per se. I used the
You can see in the illustration video below that the button works as it prints
hello but the song is not played. The sound you hear in the background is just a youtube video, to check if the recording can catch the sound.
Himmmmm, sniffing some garbage being collected XD.
So, it seems that the python Garbage Collector deletes the
audio object as soon as the function end reached.
try one of these solutions:
1. pass the parent widget
audio = QSound("..", self)
2. store it in the member variable
I prefer to store it in the __init__ and call play whenever it’s needed.
self.audio = QSound("..")
Thanks. Works as expected!
May I ask how did you come up with "garbage being collected " ?
I couldn’t diagnose it since there is no error print out.
I had similar problem when I started learning PyQt, but with QWidget not being visible.
so when I saw your code in the video I related to it.
It’s hard to detect because the object is not accessed anywhere, so no error will happen when it gets deleted, but the only symptom that its effects will vanish.
To be a away of this problem you just need to remember: always have a reference to an object, or it will be deleted, the the reference may be implicit by giving the QObject a
Ah gotcha. Thanks for clarification!
Have a great day ahead