Port QMultiMedia usage for Qt6

This commit is contained in:
Alexander Lohnau 2023-07-20 11:14:07 +03:00
parent 5456f726af
commit b8c6e529ba
2 changed files with 35 additions and 1 deletions

View file

@ -15,6 +15,10 @@
#include <QMediaPlayer> #include <QMediaPlayer>
#include <QStandardPaths> #include <QStandardPaths>
#if QT_VERSION_MAJOR == 6
#include <QAudioOutput>
#endif
K_PLUGIN_FACTORY(FindThisDeviceConfigFactory, registerPlugin<FindThisDeviceConfig>();) K_PLUGIN_FACTORY(FindThisDeviceConfigFactory, registerPlugin<FindThisDeviceConfig>();)
FindThisDeviceConfig::FindThisDeviceConfig(QObject *parent, const QVariantList &args) FindThisDeviceConfig::FindThisDeviceConfig(QObject *parent, const QVariantList &args)
@ -68,11 +72,20 @@ void FindThisDeviceConfig::playSound()
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified."; qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
} else { } else {
QMediaPlayer *player = new QMediaPlayer; QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole)); player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL); player->setMedia(soundURL);
player->setVolume(100); player->setVolume(100);
player->play(); player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater); connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
#else
auto audioOutput = new QAudioOutput();
audioOutput->setVolume(100);
player->setSource(soundURL);
player->setAudioOutput(audioOutput);
player->play();
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
} }
} }

View file

@ -20,6 +20,10 @@
#include <QDBusConnection> #include <QDBusConnection>
#include <QMediaPlayer> #include <QMediaPlayer>
#if QT_VERSION_MAJOR == 6
#include <QAudioOutput>
#endif
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json") K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args) FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args)
@ -42,9 +46,18 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
} }
QMediaPlayer *player = new QMediaPlayer; QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole)); player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL); player->setMedia(soundURL);
player->setVolume(100); player->setVolume(100);
#else
auto audioOutput = new QAudioOutput();
audioOutput->setVolume(100);
player->setSource(soundURL);
player->setAudioOutput(audioOutput);
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
player->play();
#ifndef Q_OS_WIN #ifndef Q_OS_WIN
const auto sinks = PulseAudioQt::Context::instance()->sinks(); const auto sinks = PulseAudioQt::Context::instance()->sinks();
@ -55,7 +68,11 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
mutedSinks.append(sink); mutedSinks.append(sink);
} }
} }
connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks] { #if QT_VERSION_MAJOR < 6
connect(player, &QMediaPlayer::stateChanged, this, [mutedSinks] {
#else
connect(player, &QMediaPlayer::playingChanged, this, [mutedSinks] {
#endif
for (auto sink : qAsConst(mutedSinks)) { for (auto sink : qAsConst(mutedSinks)) {
sink->setMuted(true); sink->setMuted(true);
} }
@ -63,7 +80,11 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
#endif #endif
player->play(); player->play();
#if QT_VERSION_MAJOR < 6
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater); connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
#else
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
// TODO: ensure to use built-in loudspeakers // TODO: ensure to use built-in loudspeakers
return true; return true;