Is QSound no longer available?

Heads up! You've already completed this tutorial.

bentraje6381 | 2020-11-21 03:55:23 UTC | #1

Hi,

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' or AttributeError: module 'PySide2.QtCore' has no attribute 'QtMultimedia'

Here is the code so far:

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

python
from PySide2.QtMultimedia import QSound
audio = QSound('sample.wav')
audio.play()

bentraje6381 | 2020-11-24 10:43:03 UTC | #3

@Salem_Bream

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 mp3 and wav format.

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.

https://www.dropbox.com/s/8fgu7ql1ljl1pi1/py004_pyqt_qsound_not_playing.mp4?dl=0


Salem_Bream | 2020-11-24 19:42:00 UTC | #4

Welcome,

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

python
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("..") self.audio.play()


bentraje6381 | 2020-11-24 19:42:56 UTC | #5

@Salem_Bream

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 parent.


bentraje6381 | 2020-11-24 20:18:00 UTC | #7

Ah gotcha. Thanks for clarification! Have a great day ahead :)


Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Is QSound no longer available? was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.