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
QT = core network dbus
QT = core network dbus multimedia
PKGCONFIG += nemonotifications-qt5

View file

@ -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;
}
}

View file

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