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. Can't play again QSoundEffect when inside a QThread
Forum Updated to NodeBB v4.3 + New Features

Can't play again QSoundEffect when inside a QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 870 Views
  • 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.
  • M Offline
    M Offline
    mipr
    wrote on last edited by mipr
    #1

    Hi,

    I have created a class with a QSoundEffect and a QThread inside, the goal is to play the sound inside the thread.
    My code worked, I can play the sound once thanks to this code :

    SoundAlert.h :

    #ifndef SOUNDALERT_H
    #define SOUNDALERT_H
    
    #include <QObject>
    #include <QSoundEffect>
    
    class SoundAlert : public QObject
    {
     Q_OBJECT
     public:
        SoundAlert(QUrl soundUrl);
        ~SoundAlert();
     public slots:
        void play();
    
     private:
        QSoundEffect* _sound;
        QThread* _thread;
    };
    
    #endif // SOUNDALERT_H
    

    SoundAlert.cpp :

    #include "SoundAlert.h"
    #include "MyApplication.h"
    
    SoundAlert::SoundAlert(QUrl soundUrl){
     _sound = new QSoundEffect;
     _sound->setSource(soundUrl);
     _thread = new QThread;
     _sound->moveToThread(_thread);
     _thread->start();
    
     QObject::connect(_sound, &QSoundEffect;::playingChanged, this, [=](){
       qDebug() << "SoundAlert playingChanged:" << _sound->isPlaying();
     });
    }
    
    SoundAlert::~SoundAlert(){
     _thread->quit();
     _sound->deleteLater();
     _thread->deleteLater();
    }
    
    void SoundAlert::play(){
     if(_sound->status() == _sound->Ready && !_sound->isPlaying()){
       _sound->play();
     }
    }
    

    However, if I try to play the sound many times thanks to a recurring signal, it plays just once.

    The test !_sound->isPlaying() is used to avoid stopping sound before the end.

    The issue is the playing attribut of the QSoundEffect that is never change to false. I've tried without the QThread and I have no issue, it plays the sound many times and I can see the playing attribut as false. Do you have an idea?

    My Qt version is 5.15.7 and I am on Windows.

    Edit: I modified the code to be more complete.

    Christian EhrlicherC 1 Reply Last reply
    0
    • M mipr

      Hi,

      I have created a class with a QSoundEffect and a QThread inside, the goal is to play the sound inside the thread.
      My code worked, I can play the sound once thanks to this code :

      SoundAlert.h :

      #ifndef SOUNDALERT_H
      #define SOUNDALERT_H
      
      #include <QObject>
      #include <QSoundEffect>
      
      class SoundAlert : public QObject
      {
       Q_OBJECT
       public:
          SoundAlert(QUrl soundUrl);
          ~SoundAlert();
       public slots:
          void play();
      
       private:
          QSoundEffect* _sound;
          QThread* _thread;
      };
      
      #endif // SOUNDALERT_H
      

      SoundAlert.cpp :

      #include "SoundAlert.h"
      #include "MyApplication.h"
      
      SoundAlert::SoundAlert(QUrl soundUrl){
       _sound = new QSoundEffect;
       _sound->setSource(soundUrl);
       _thread = new QThread;
       _sound->moveToThread(_thread);
       _thread->start();
      
       QObject::connect(_sound, &QSoundEffect;::playingChanged, this, [=](){
         qDebug() << "SoundAlert playingChanged:" << _sound->isPlaying();
       });
      }
      
      SoundAlert::~SoundAlert(){
       _thread->quit();
       _sound->deleteLater();
       _thread->deleteLater();
      }
      
      void SoundAlert::play(){
       if(_sound->status() == _sound->Ready && !_sound->isPlaying()){
         _sound->play();
       }
      }
      

      However, if I try to play the sound many times thanks to a recurring signal, it plays just once.

      The test !_sound->isPlaying() is used to avoid stopping sound before the end.

      The issue is the playing attribut of the QSoundEffect that is never change to false. I've tried without the QThread and I have no issue, it plays the sound many times and I can see the playing attribut as false. Do you have an idea?

      My Qt version is 5.15.7 and I am on Windows.

      Edit: I modified the code to be more complete.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mipr said in Can't play again QSoundEffect when inside a QThread:

      My Qt version is 5.15.7 and I am on Windows.

      Since this is a commercial version you should also ask the Qt support for help.
      A fully, minimal example would be good though.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        @mipr said in Can't play again QSoundEffect when inside a QThread:

        My Qt version is 5.15.7 and I am on Windows.

        Since this is a commercial version you should also ask the Qt support for help.
        A fully, minimal example would be good though.

        M Offline
        M Offline
        mipr
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks for your answer.

        I have just contacted Qt support, I don't usually use this method. Thanks for your advice!

        I changed the code of my original post. I hope having these complete files can help, I can't release more as the application is quite large.
        The signal connected to the play() method (of SoundAlert) is equivalent to a QTimer with an infinite loop.

        Christian EhrlicherC 1 Reply Last reply
        0
        • M mipr

          @Christian-Ehrlicher Thanks for your answer.

          I have just contacted Qt support, I don't usually use this method. Thanks for your advice!

          I changed the code of my original post. I hope having these complete files can help, I can't release more as the application is quite large.
          The signal connected to the play() method (of SoundAlert) is equivalent to a QTimer with an infinite loop.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mipr said in Can't play again QSoundEffect when inside a QThread:

          The signal connected to the play() method (of SoundAlert) is equivalent to a QTimer with an infinite loop.

          Can you show us the connection?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @mipr said in Can't play again QSoundEffect when inside a QThread:

            The signal connected to the play() method (of SoundAlert) is equivalent to a QTimer with an infinite loop.

            Can you show us the connection?

            M Offline
            M Offline
            mipr
            wrote on last edited by mipr
            #5

            @Christian-Ehrlicher I tried with a QTimer instead of the other connection (because it is hard to understand without the context) and I find the same behavior.

            SoundAlert* soundAlert = new SoundAlert(
                QUrl::fromLocalFile(":/resources/sounds/mySound.wav"));
            QTimer* testTimer = new QTimer();
            testTimer->setInterval(2000);
            QObject::connect(testTimer, &QTimer::timeout, soundAlert, &SoundAlert::play);
            testTimer->start();
            

            Without the thread part, the sound is played many times. And with, the sound is played once.

            When I remove the thread part I just comment these two lines (SoundAlert.cpp):

            //_sound->moveToThread(_thread);
            //_thread->start();
            
            1 Reply Last reply
            0

            • Login

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