bentraje6381 | 2020-11-21 03:55:23 UTC | #1
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') audio.play()
Salem_Bream | 2020-11-23 18:09:46 UTC | #2
Hi, 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') audio.play()
bentraje6381 | 2020-11-24 10:43:03 UTC | #3
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.
Salem_Bream | 2020-11-24 19:42:00 UTC | #4
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) audio.play()
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("..")
bentraje6381 | 2020-11-24 19:42:56 UTC | #5
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.
Salem_Bream | 2020-11-24 20:06:06 UTC | #6
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
bentraje6381 | 2020-11-24 20:18:00 UTC | #7
Ah gotcha. Thanks for clarification! Have a great day ahead :)