Following code isn't working. QSoundEffect::status always return QSoundEffect::Loading and QSoundEffect::isLoaded return false.
QFile file("file.wav");
file.open(QIODevice::ReadWrite);
QByteArray data = file.readAll();
file.close();
QSoundEffect sound;
sound.setSource(QUrl::fromEncoded(data));
sound.setVolume(1.0f);
sound.play();
I don't know if it's a bug or it's just a lack of information in the documentation, but the fact is that you need to set a parent for your
QSoundEffect.For example, if you put your code in the
mainfunction, it won't work, as I suppose you already checked.However, if you write the following code, where the
QSoundEffectobject has aQWidgetas parent, you will hear the sound correctly.(I uploaded the complete example to GitHub)
main.cpp
widget.h
widget.cpp