From 07c463b23833fc6745949f9e674e7dbeae64c5a0 Mon Sep 17 00:00:00 2001 From: Matti Viljanen Date: Sun, 27 Dec 2020 16:17:05 +0200 Subject: [PATCH] Use QMediaPlayer instead of QSoundeffect ...but it STILL dowsn't respect ringtone volume... --- service/src/mynotification.cpp | 12 +++++++----- service/src/mynotification.h | 7 +++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/service/src/mynotification.cpp b/service/src/mynotification.cpp index 1311fae..3033c43 100644 --- a/service/src/mynotification.cpp +++ b/service/src/mynotification.cpp @@ -22,7 +22,9 @@ MyNotification::MyNotification(QObject* parent) : QObject(parent) notification.setAppName("Battery Buddy"); notification.setAppIcon("harbour-batterybuddy"); playSound = false; - connect(&sound, SIGNAL(loadedChanged()), &sound, SLOT(play())); + sound.setAudioRole(QAudio::NotificationRole); + connect(&sound, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), + this, SLOT(soundLoadedChanged(QMediaPlayer::MediaStatus))); } MyNotification::~MyNotification() @@ -36,9 +38,9 @@ void MyNotification::send(QString title, QString body, QString soundFile) body = body.replace("\"", "\\\""); playSound = true; - if(sound.source() != QUrl::fromLocalFile(soundFile)) { + if(sound.media() != QUrl::fromLocalFile(soundFile)) { // Signalled to play() - sound.setSource(QUrl::fromLocalFile(soundFile)); + sound.setMedia(QUrl::fromLocalFile(soundFile)); } else if (playSound){ // Must manually trigger play() @@ -61,8 +63,8 @@ void MyNotification::close() return; } -void MyNotification::soundLoadedChanged() { - if(playSound && sound.status() == QSoundEffect::Ready) { +void MyNotification::soundLoadedChanged(QMediaPlayer::MediaStatus newStatus) { + if(playSound && newStatus == QMediaPlayer::LoadedMedia) { sound.play(); playSound = false; } diff --git a/service/src/mynotification.h b/service/src/mynotification.h index 4f2852e..5032b4b 100644 --- a/service/src/mynotification.h +++ b/service/src/mynotification.h @@ -19,9 +19,8 @@ #define MYNOTIFICATION_H #include -#include #include -#include +#include #include #include @@ -40,11 +39,11 @@ public slots: private: QString noteID = "1"; Notification notification; - QSoundEffect sound; + QMediaPlayer sound; bool playSound; private slots: - void soundLoadedChanged(); + void soundLoadedChanged(QMediaPlayer::MediaStatus newStatus); }; #endif // MYNOTIFICATION_H