bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
{
Q_UNUSED(np);
const QString soundFile = config()->getString(QStringLiteral("ringtone"), defaultSound());
const QUrl soundURL = QUrl::fromLocalFile(soundFile);
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
return true;
}
QMediaPlayer *player = new QMediaPlayer;
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
player->setVolume(100);
#ifndef Q_OS_WIN
const auto sinks = PulseAudioQt::Context::instance()->sinks();
QVector mutedSinks;
for (auto sink : sinks) {
if (sink->isMuted()) {
sink->setMuted(false);
mutedSinks.append(sink);
}
}
connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks] {
for (auto sink : qAsConst(mutedSinks)) {
sink->setMuted(true);
}
});
#endif
player->play();
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
// TODO: ensure to use built-in loudspeakers
return true;
}
This method creates a QMediaPlayer object from a NetworkPacket object. It takes the ringtone config from the plugin. It constructs the sound file, sets the volume 100% of the sound and sets the muted state to true. It also connects the stateChanged signal to the stateChanged signal.