During the testing of QSound for one of my programs, I encountered an issue I can't get my head around. Whenever I'm implementing QSound objects inside another object, the play() function doesn't seem to be able to call the member variables of the QSound object.
Here are the examples, I used to analyze the problem:
- Example with QSound in Script (Works as expected with 2 repetitions of the sound)
from PyQt5.Qt import QApplication
from PyQt5.QtMultimedia import QSound
import sys
app=QApplication(sys.argv)
SoundObject=QSound("./path/sound.wav")
SoundObject.play()
SoundObject.play("./path/sound.wav")
sys.exit(app.exec())
- Example QSound inside another object (Only one repetition of the sound)
from PyQt5.Qt import QApplication
from PyQt5.QtMultimedia import QSound
import sys
app=QApplication(sys.argv)
class SoundClass:
def __init__(self):
SoundObject = QSound("./path/sound.wav")
print(SoundObject.fileName()) # output= "./path/sound.wav"
SoundObject.play() # this doesn't do anything
SoundObject.play("./path/sound.wav")
SoundClass()
sys.exit(app.exec())
In both cases I would expect, that the sound gets played 2 times in a row. However as soon as I use an QSound object inside another object, it seems like the ".play()" function ignores the settings of my object and instead calls the static function QSound.play(). The same behavior appears with other member variables of the QSound object (e.g. setLoops/loops).
While searching for an answer to my problem, I found a thread about the same issue with C++ and Qt. Although someone mentioned that QSound might be deprecated, there wasn't a definite answer. (And I'm wondering why it would 4 years later still be part of the documentation.)
Link to the Thread: QSound::play("soundpath") call works but a QSound object doesn't
I used the following documentation: https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtmultimedia/qsound.html?highlight=qsound#PyQt5.QtMultimedia.QSound
My specs:
- Windows 10.0.17134.706
- Python 3.7
- PyQt 5.12
Am I missing something crucial or is this simply a bug?