Use QSoundEffect for notification sounds

This commit is contained in:
Matti Viljanen 2020-12-27 02:10:05 +02:00
parent 4d58c55041
commit ff50092d84
3 changed files with 25 additions and 12 deletions

View file

@ -2,7 +2,7 @@ TARGET = harbour-batterybuddy-daemon
CONFIG = sailfishapp qt console c++11 sailfish_build CONFIG = sailfishapp qt console c++11 sailfish_build
QT = core network dbus QT = core network dbus multimedia
PKGCONFIG += nemonotifications-qt5 PKGCONFIG += nemonotifications-qt5

View file

@ -21,6 +21,8 @@ MyNotification::MyNotification(QObject* parent) : QObject(parent)
{ {
notification.setAppName("Battery Buddy"); notification.setAppName("Battery Buddy");
notification.setAppIcon("harbour-batterybuddy"); notification.setAppIcon("harbour-batterybuddy");
playSound = false;
connect(&sound, SIGNAL(loadedChanged()), &sound, SLOT(play()));
} }
MyNotification::~MyNotification() MyNotification::~MyNotification()
@ -33,13 +35,15 @@ void MyNotification::send(QString title, QString body, QString soundFile)
title = title.replace("\"", "\\\""); title = title.replace("\"", "\\\"");
body = body.replace("\"", "\\\""); body = body.replace("\"", "\\\"");
QStringList args; playSound = true;
if(sound.source() != QUrl::fromLocalFile(soundFile)) {
QProcess aplay; // Signalled to play()
if(!soundFile.isEmpty()) { sound.setSource(QUrl::fromLocalFile(soundFile));
QStringList aplayArgs; }
aplayArgs << soundFile; else if (playSound){
aplay.start("paplay", aplayArgs); // Must manually trigger play()
sound.play();
playSound = false;
} }
notification.setSummary(title); notification.setSummary(title);
@ -48,10 +52,6 @@ void MyNotification::send(QString title, QString body, QString soundFile)
notification.setPreviewBody(body); notification.setPreviewBody(body);
notification.publish(); 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; return;
} }
@ -60,3 +60,10 @@ void MyNotification::close()
notification.close(); notification.close();
return; return;
} }
void MyNotification::soundLoadedChanged() {
if(playSound && sound.status() == QSoundEffect::Ready) {
sound.play();
playSound = false;
}
}

View file

@ -21,6 +21,7 @@
#include <QObject> #include <QObject>
#include <QProcess> #include <QProcess>
#include <QTimer> #include <QTimer>
#include <QSoundEffect>
#include <nemonotifications-qt5/notification.h> #include <nemonotifications-qt5/notification.h>
#include <QDebug> #include <QDebug>
@ -39,6 +40,11 @@ public slots:
private: private:
QString noteID = "1"; QString noteID = "1";
Notification notification; Notification notification;
QSoundEffect sound;
bool playSound;
private slots:
void soundLoadedChanged();
}; };
#endif // MYNOTIFICATION_H #endif // MYNOTIFICATION_H