diff --git a/src/clientdlg.cpp b/src/clientdlg.cpp index 09409d7eaf..b102c34724 100644 --- a/src/clientdlg.cpp +++ b/src/clientdlg.cpp @@ -876,9 +876,7 @@ void CClientDlg::OnChatTextReceived ( QString strChatText ) { if ( pSettings->bEnableAudioAlerts ) { - QSoundEffect* sf = new QSoundEffect(); - sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) ); - sf->play(); + PlayAudioAlert ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_message.wav" ) ); } ChatDlg.AddChatText ( strChatText ); @@ -927,9 +925,7 @@ void CClientDlg::OnNumClientsChanged ( int iNewNumClients ) { if ( pSettings->bEnableAudioAlerts && iNewNumClients > iClients ) { - QSoundEffect* sf = new QSoundEffect(); - sf->setSource ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_user.wav" ) ); - sf->play(); + PlayAudioAlert ( QUrl::fromLocalFile ( ":sounds/res/sounds/new_user.wav" ) ); } // iNewNumClients will be zero on the first trigger of this signal handler when connecting to a new server. diff --git a/src/clientdlg.h b/src/clientdlg.h index 687fe811f4..6a34cc66f0 100644 --- a/src/clientdlg.h +++ b/src/clientdlg.h @@ -60,7 +60,6 @@ #include #include #include -#include #if QT_VERSION >= QT_VERSION_CHECK( 5, 6, 0 ) # include #endif diff --git a/src/util.cpp b/src/util.cpp index 90b1506206..eba8ce47a4 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -758,6 +758,19 @@ QSize CMinimumStackedLayout::sizeHint() const } return QStackedLayout::sizeHint(); } + +void PlayAudioAlert ( QUrl soundUrl ) +{ + QSoundEffect* sf = new QSoundEffect(); + QObject::connect ( sf, &QSoundEffect::playingChanged, sf, [sf]() { + if ( !sf->isPlaying() ) + { + sf->deleteLater(); + } + } ); + sf->setSource ( soundUrl ); + sf->play(); +} #endif /******************************************************************************\ diff --git a/src/util.h b/src/util.h index c104d7ede7..84ce8e3261 100644 --- a/src/util.h +++ b/src/util.h @@ -75,6 +75,7 @@ #include #include #include + #ifndef DISABLE_SRV_DNS # include #endif @@ -94,6 +95,7 @@ # include # include # include +# include # include "ui_aboutdlgbase.h" #endif @@ -489,6 +491,8 @@ class CMinimumStackedLayout : public QStackedLayout CMinimumStackedLayout ( QWidget* parent = nullptr ) : QStackedLayout ( parent ) {} virtual QSize sizeHint() const override; }; + +void PlayAudioAlert ( QUrl soundUrl ); #endif /******************************************************************************\