Ulfric Storm
Ulfric Storm

Reputation: 129

No Sound Output with QSoundEffect in Qt5.0.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?

Upvotes: 2

Views: 4393

Answers (5)

Dtor
Dtor

Reputation: 609

The QSoundEffect::play() method starts the sound playing, but returns immediately. Your code sample doesn't show everything, but it does appear that the QSoundEffect alarm is a local variable and I'm guessing that you exit the method shortly after the play() call. This causes the alarm variable to go out of scope, it's destructor is called, and the play sound stops (probably before it had time to make any sound). The QMessageBox masks the problem because it blocks until you close the message box, so the alarm variable doesn't go out of scope and stop playing until then.

Upvotes: 0

Edward
Edward

Reputation: 159

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();
}

Upvotes: 1

Boudabiee
Boudabiee

Reputation: 1

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.

Upvotes: 0

Tower Jimmy
Tower Jimmy

Reputation: 567

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

Upvotes: 0

Armand
Armand

Reputation: 774

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.

Upvotes: 0

Related Questions