Ok, I found a solution to my problem. This is the PlaySound method from my SoundEffect class. The area of interest is the code below the note.
void SoundEffect::PlaySound() {
mPlayer.setSource(soundUrl);
// Note about QMediaPlayer::Loops
// Using QMediaPlayer::Loops::Infinite does not work correctly on Linux and Windows.
// It either plays once and then stops or plays multiple times, but stops unexpectedly.
// Therefore, use QMediaPlayer::Loops::Once and connect to the playbackStateChanged signal.
// The slot will reset the QMediaPlayer and play the sound again.
mPlayer.setLoops(QMediaPlayer::Loops::Once);
connect(&mPlayer, &QMediaPlayer::playbackStateChanged,
this, [this, soundUrl](QMediaPlayer::PlaybackState aState) {
if (aState == QMediaPlayer::PlaybackState::StoppedState) {
// Clear the source
mPlayer.setSource(QUrl());
// Restore the source
mPlayer.setSource(soundUrl);
mPlayer.play();
}
});
mPlayer.play();
}