Replaced deprecated QSound and QAudioDeviceInfo by QAudioDevice and QMediaDevices for Qt 6

This commit is contained in:
thunder2 2025-07-18 18:33:23 +02:00
parent 15684bf8d3
commit 95127592d7
2 changed files with 32 additions and 4 deletions

View file

@ -21,12 +21,22 @@
#include <QApplication> #include <QApplication>
#include <QFile> #include <QFile>
#include <QProcess> #include <QProcess>
#include <QSound>
#include <QDir> #include <QDir>
#if QT_VERSION >= QT_VERSION_CHECK (6, 0, 0)
#include <QAudioDevice>
#include <QMediaDevices>
#include <QUrl>
#elif QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
#include <QAudioDeviceInfo>
#endif
#if QT_VERSION < QT_VERSION_CHECK (6, 0, 0)
#include <QSound>
#endif
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
#include <QAudio> #include <QAudio>
#include <QAudioDeviceInfo>
#endif #endif
// #ifdef QMEDIAPLAYER // #ifdef QMEDIAPLAYER
@ -242,14 +252,24 @@ void SoundManager::playFile(const QString &filename)
QString playFilename = realFilename(filename); QString playFilename = realFilename(filename);
bool played = false ; bool played = false ;
#if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK (6, 0, 0)
if (!QMediaDevices::audioOutputs().isEmpty())
#elif QT_VERSION >= QT_VERSION_CHECK (5, 0, 0)
if (!QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).isEmpty()) if (!QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).isEmpty())
#else #else
if (QSound::isAvailable()) if (QSound::isAvailable())
#endif #endif
{ {
#if QT_VERSION >= QT_VERSION_CHECK (6, 0, 0)
if (soundManager) {
soundManager->mSoundEffect.setSource(QUrl::fromLocalFile(playFilename));
soundManager->mSoundEffect.play();
played = true;
}
#else
QSound::play(playFilename); QSound::play(playFilename);
played = true ; played = true ;
#endif
} }
if(!played) // let's go for the hard core stuff if(!played) // let's go for the hard core stuff

View file

@ -24,6 +24,10 @@
#include <QObject> #include <QObject>
#include <QMap> #include <QMap>
#if QT_VERSION >= QT_VERSION_CHECK (6, 0, 0)
#include <QSoundEffect>
#endif
#define SOUND_NEW_CHAT_MESSAGE "NewChatMessage" #define SOUND_NEW_CHAT_MESSAGE "NewChatMessage"
#define SOUND_USER_ONLINE "User_go_Online" #define SOUND_USER_ONLINE "User_go_Online"
#define SOUND_MESSAGE_ARRIVED "MessageArrived" #define SOUND_MESSAGE_ARRIVED "MessageArrived"
@ -91,6 +95,10 @@ public:
private: private:
SoundManager(); SoundManager();
#if QT_VERSION >= QT_VERSION_CHECK (6, 0, 0)
QSoundEffect mSoundEffect;
#endif
}; };
extern SoundManager *soundManager; extern SoundManager *soundManager;