From ff50092d849abb8d5a53154fc728803c5d8c7cb6 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 27 Dec 2020 02:10:05 +0200 Subject: [PATCH] Use QSoundEffect for notification sounds --- service/service.pro | 2 +- service/src/mynotification.cpp | 29 ++++++++++++++++++----------- service/src/mynotification.h | 6 ++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/service/service.pro b/service/service.pro index 6a12908..bc71ece 100644 --- a/service/service.pro +++ b/service/service.pro @@ -2,7 +2,7 @@ TARGET = harbour-batterybuddy-daemon CONFIG = sailfishapp qt console c++11 sailfish_build -QT = core network dbus +QT = core network dbus multimedia PKGCONFIG += nemonotifications-qt5 diff --git a/service/src/mynotification.cpp b/service/src/mynotification.cpp index 4dbf423..1311fae 100644 --- a/service/src/mynotification.cpp +++ b/service/src/mynotification.cpp @@ -21,6 +21,8 @@ MyNotification::MyNotification(QObject* parent) : QObject(parent) { notification.setAppName("Battery Buddy"); notification.setAppIcon("harbour-batterybuddy"); + playSound = false; + connect(&sound, SIGNAL(loadedChanged()), &sound, SLOT(play())); } MyNotification::~MyNotification() @@ -33,13 +35,15 @@ void MyNotification::send(QString title, QString body, QString soundFile) title = title.replace("\"", "\\\""); body = body.replace("\"", "\\\""); - QStringList args; - - QProcess aplay; - if(!soundFile.isEmpty()) { - QStringList aplayArgs; - aplayArgs << soundFile; - aplay.start("paplay", aplayArgs); + playSound = true; + if(sound.source() != QUrl::fromLocalFile(soundFile)) { + // Signalled to play() + sound.setSource(QUrl::fromLocalFile(soundFile)); + } + else if (playSound){ + // Must manually trigger play() + sound.play(); + playSound = false; } notification.setSummary(title); @@ -48,10 +52,6 @@ void MyNotification::send(QString title, QString body, QString soundFile) notification.setPreviewBody(body); notification.publish(); - // Playing the sound may take a while, so let's do this as late as possible. - // Shouldn't matter though, because the minimum delay is 1:00 - // and the sound plays for a few seconds. - aplay.waitForFinished(); return; } @@ -60,3 +60,10 @@ void MyNotification::close() notification.close(); return; } + +void MyNotification::soundLoadedChanged() { + if(playSound && sound.status() == QSoundEffect::Ready) { + sound.play(); + playSound = false; + } +} diff --git a/service/src/mynotification.h b/service/src/mynotification.h index 030bab6..4f2852e 100644 --- a/service/src/mynotification.h +++ b/service/src/mynotification.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -39,6 +40,11 @@ public slots: private: QString noteID = "1"; Notification notification; + QSoundEffect sound; + bool playSound; + +private slots: + void soundLoadedChanged(); }; #endif // MYNOTIFICATION_H