Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. No sound with QSound or QSoundEffect without using QEventLoop
Forum Updated to NodeBB v4.3 + New Features

No sound with QSound or QSoundEffect without using QEventLoop

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 2.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oreonan
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      QApplication::exec() and QGuiApplication::exec() run an event loop. Use one of these to show the GUI and play sound at the same time, instead of using QEventLoop.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      2
      • O Offline
        O Offline
        Oreonan
        wrote on last edited by
        #3

        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...

        jeremy_kJ 1 Reply Last reply
        0
        • O Oreonan

          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...

          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

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

          Asking a question about code? http://eel.is/iso-c++/testcase/

          1 Reply Last reply
          0
          • O Offline
            O Offline
            Oreonan
            wrote on last edited by
            #5

            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?

            jeremy_kJ 1 Reply Last reply
            0
            • O Oreonan

              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?

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              @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, or QApplication 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.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              0
              • O Offline
                O Offline
                Oreonan
                wrote on last edited by
                #7

                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...

                jeremy_kJ 1 Reply Last reply
                0
                • O Oreonan

                  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...

                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on last edited by jeremy_k
                  #8

                  @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.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  1
                  • O Offline
                    O Offline
                    Oreonan
                    wrote on last edited by
                    #9

                    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?

                    jeremy_kJ 1 Reply Last reply
                    0
                    • O Oreonan

                      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?

                      jeremy_kJ Offline
                      jeremy_kJ Offline
                      jeremy_k
                      wrote on last edited by
                      #10

                      @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.

                      Asking a question about code? http://eel.is/iso-c++/testcase/

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        Oreonan
                        wrote on last edited by
                        #11

                        No, it's not "initial program" but "initial problem" :), my mistake :)

                        mrjjM 1 Reply Last reply
                        0
                        • O Oreonan

                          No, it's not "initial program" but "initial problem" :), my mistake :)

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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.

                          1 Reply Last reply
                          1

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved