Use QMediaPlayer instead of QSoundeffect

...but it STILL dowsn't respect ringtone volume...
This commit is contained in:
Matti Viljanen 2020-12-27 16:17:05 +02:00
parent 284c46196e
commit 07c463b238
2 changed files with 10 additions and 9 deletions

View file

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

View file

@ -19,9 +19,8 @@
#define MYNOTIFICATION_H
#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QSoundEffect>
#include <QMediaPlayer>
#include <nemonotifications-qt5/notification.h>
#include <QDebug>
@ -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