No sound with QSound or QSoundEffect without using QEventLoop
-
Hi,
I'm trying to play in my app, for this I want to use QSound, or QSoundEffect.
I made the following codes:
QSoundEffect effect;
effect.setSource(QUrl::fromLocalFile(myfile));
QSoundEffect.play();
But I have no sound.
I also tried this:
QSound effect(myfile);
effect.play();
Nothing again.
I added:
effect.setLoop(3)
Or:
effect.setLoops(3);
And:
QEventLoop;
loop.exec();
With this I hear sound, but if I run that when my app starts, GUI don't appear.
Have you an idea?
Thanks. -
-
I tried the following:
QApplication::exec();QApplication::exec(effect);
QEventLoop loop;
QApplication::exec(loop);But always the same error when building:
incomplete type 'QApplication' used in nested name specifier
I not really understand why...The compiler error suggests an incorrect combination of namespaces, include files, and QApplication object instantiation. We can try to sort it out with a complete minimal example that demonstrates the error.
A basic program should look something like:
#include <QApplication> #include <QPushButton> #include <QSound> #include <QObject> int main(int argc, char* argv[]) { QApplication app(argc, argv); QSound sound("/tmp/test.wav"); sound.setLoops(QSound::Infinite); sound.play(); QPushButton button("Push to close"); QObject::connect(&button, &QPushButton::clicked, &app, &QCoreApplication::quit); button.show(); return app.exec(); }
-
OK, so it's probably due to my function isn't in a file where is a QObject class herited.
I try to explain:
I have some files, mainwindow.cpp, textmessage.cpp, ...
I include in all of this files common.cpp which provide a lot of functions, including playsound.
But I probably can't use QApplication::exec in my common.cpp file.
I don't know if I'm really understandable? -
OK, so it's probably due to my function isn't in a file where is a QObject class herited.
I try to explain:
I have some files, mainwindow.cpp, textmessage.cpp, ...
I include in all of this files common.cpp which provide a lot of functions, including playsound.
But I probably can't use QApplication::exec in my common.cpp file.
I don't know if I'm really understandable?@Oreonan said in No sound with QSound or QSoundEffect without using QEventLoop:
But I probably can't use QApplication::exec in my common.cpp file.
Not having a
QCoreApplication
,QGuiApplication
, orQApplication
removes most of the functionality of Qt. If a place can't be found to construct one instance of the appropriate class, the program must be refactored. -
I have QApplication, but not in my function which play a sound.
For example, I have mainwindow.cpp, which include and use QApplication, in a function of this mainwindow class I call playsound, which is a function declared in common.cpp (which not use QApplication).
So if I add QApplication::exec() in my playsound function in common.cpp I have the error I copied before... -
I have QApplication, but not in my function which play a sound.
For example, I have mainwindow.cpp, which include and use QApplication, in a function of this mainwindow class I call playsound, which is a function declared in common.cpp (which not use QApplication).
So if I add QApplication::exec() in my playsound function in common.cpp I have the error I copied before...@Oreonan said in No sound with QSound or QSoundEffect without using QEventLoop:
I have QApplication, but not in my function which play a sound.
For example, I have mainwindow.cpp, which include and use QApplication, in a function of this mainwindow class I call playsound, which is a function declared in common.cpp (which not use QApplication).The QApplication needs to be created first, in the thread that attempts to play the sound. It needs to continue to exist until all other Qt functionality has stopped. Files do not matter at runtime.
So if I add QApplication::exec() in my playsound function in common.cpp I have the error I copied before...
Without the full text of the error and the source code, it's nearly impossible to help. The only other advice I can offer is to start from the working example above.
-
Hmmmm, I think I can't use like this in my program, I need to play a sound for some periodic events, so if I use this like this I have to call a QApplication everytime...
We can't play a sound with QSoundEffect without event loop?
Initial program is: currently I have QSound::play(myfile);, but if another sound need to be played it's play in same time of first sound, maybe we have a way to force QSound to wait until first sound is finished to play another one? -
Hmmmm, I think I can't use like this in my program, I need to play a sound for some periodic events, so if I use this like this I have to call a QApplication everytime...
We can't play a sound with QSoundEffect without event loop?
Initial program is: currently I have QSound::play(myfile);, but if another sound need to be played it's play in same time of first sound, maybe we have a way to force QSound to wait until first sound is finished to play another one?@Oreonan said in No sound with QSound or QSoundEffect without using QEventLoop:
Hmmmm, I think I can't use like this in my program, I need to play a sound for some periodic events, so if I use this like this I have to call a QApplication everytime...
Usually the same QApplication is used for the entire program. Create it on the stack at the beginning of main().
We can't play a sound with QSoundEffect without event loop?
No.
Initial program is: currently I have QSound::play(myfile);,
That's not a valid C++ program, unless you are working with an unhosted environment.
-
@Oreonan
Hi
But why don't you have the normal setup where main.cpp
creates a QApplication ?- but if another sound need to be played it's play in same time of first sound, maybe we have a way to - force QSound to wait until first sound is finished to play another one?
As far as i know, it wont emit signal when finished but you can use
https://doc.qt.io/qt-5/qsound.html#isFinished
to check.So if you make a small handler class to play sounds that will keep a list of sounds to play and use a timer to check if a song is done and then start next.