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 <QStandardPaths>
#if QT_VERSION_MAJOR == 6
#include <QAudioOutput>
#endif
K_PLUGIN_FACTORY(FindThisDeviceConfigFactory, registerPlugin<FindThisDeviceConfig>();)
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.";
} else {
QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
player->setVolume(100);
player->play();
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 <QMediaPlayer>
#if QT_VERSION_MAJOR == 6
#include <QAudioOutput>
#endif
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args)
@ -42,9 +46,18 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
}
QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
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
const auto sinks = PulseAudioQt::Context::instance()->sinks();
@ -55,7 +68,11 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
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)) {
sink->setMuted(true);
}
@ -63,7 +80,11 @@ bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
#endif
player->play();
#if QT_VERSION_MAJOR < 6
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
#else
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
// TODO: ensure to use built-in loudspeakers
return true;