Reputation: 995
How can I make sure that a sound does not start to play if another one is already playing?
Im using the following code to play sounds:
void MainWindow::displayNotification(QString message)
{
// Play sound notification
QSound *sound = new QSound("://2_1.wav", this);
sound->play();
}
Any ideas?
Upvotes: 0
Views: 238
Reputation: 957
the class "QSound" has the method "isFinished()" for that reason.
if(m_pSound->isFinished())
//play
else
//wait
Upvotes: 0
Reputation: 27611
Store a pointer to the playing QSound in your MainWindow, then check if it has finished with the isFinished function.
void MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
QString audiofile("://2_1.wav");
m_pSound = new QSound(audiofile, this);
if(!m_pSound)
{
qDebug() << "Failed to initialise sound file: " << audiofile;
}
}
void MainWindow::displayNotification(QString message)
{
if(!m_pSound) // check m_pSound is initialised
return;
// check if sound is playing
if(!m_pSound->isFinished)
return;
// Play sound notification
m_pSound->play();
}
Note that m_pSound is now declared as a member variable of MainWindow.
Upvotes: 1