2

I want to play a wav file if a singal is emitted. For testing i created a pushButton and filled its slot with

QSoundEffect alarm;
alarm.setSource(QUrl::fromLocalFile("001.wav"));
alarm.setLoopCount(QSoundEffect::Infinite);
alarm.setVolume(1.0f);
alarm.play();

But there is no sound if i click on the pushButton. After searching i found people having the same problem with Qt5 but no solution.

Whats the best and simplest solution with Qt5 to just play an uncompressed sound file (wav) and change its volume?

EDIT: I just found out that it works if i add e.g. a QMessageBox (while this message box is open):

QSoundEffect alarm;
alarm.setSource(QUrl::fromLocalFile("001.wav"));
alarm.setLoopCount(2);
alarm.setVolume(0.5f);
alarm.play();

QMessageBox::information(this, "test", "it works");

Has anybody an explanation for this behaviour?

Ulfric Storm
  • 129
  • 2
  • 12
  • sudo apt-get install libqt5multimedia5-plugins http://stackoverflow.com/questions/21939759/qaudiodeviceinfo-finds-no-default-audio-device-on-ubuntu –  Jun 02 '16 at 01:49

4 Answers4

1

for sound need to write event loop, e.g.:

void MainWindow::on_pushButton_4_clicked()
{
    QSoundEffect beep;
    beep.setSource(QUrl("qrc:/sound/beep-02.wav"));
    beep.setVolume(1.0f);
    beep.play();
    QEventLoop loop;
    loop.exec();
}
Edward
  • 149
  • 7
0

If you are under Mac OS X you could try this : QT5 QSound does not play all wave files

But the bug has been reported and should be fixed in Qt 5.1.1, so be patient.

Community
  • 1
  • 1
Armand
  • 726
  • 11
  • 29
0

yea, it's buggie.

I recommend to you to use another Audio Converter.

I used this website and it worked afterwards.

http://www.online-convert.com

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Tower Jimmy
  • 567
  • 1
  • 4
  • 15
  • Also consider using Audacity for quick conversion. Import all audio files and use "export multiple" feature to generate a set of Wave files that are played flawlessly by `QSoundEffect`. – f4lco Jan 09 '14 at 16:55
0

Your QSoundEffect needs a parent to work correctly, actually it works only when you open the messagebox because it will take it as a parent.