Can't play again QSoundEffect when inside a QThread
-
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.
-
@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. -
@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 theplay()
method (of SoundAlert) is equivalent to a QTimer with an infinite loop. -
@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?
-
@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();