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 <QFile>
#include <QProcess>
#include <QSound>
#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)
#include <QAudio>
#include <QAudioDeviceInfo>
#endif
// #ifdef QMEDIAPLAYER
@ -241,15 +251,25 @@ void SoundManager::playFile(const QString &filename)
QString playFilename = realFilename(filename);
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())
#else
if (QSound::isAvailable())
#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);
played = true ;
#endif
}
if(!played) // let's go for the hard core stuff

View file

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