remove stopping sounds from interrupting each other (#4640)

This commit is contained in:
ebbit1q 2022-06-09 03:06:44 +02:00 committed by GitHub
parent 3e5b7cd392
commit afbd7252ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,7 +9,7 @@
#define DEFAULT_THEME_NAME "Default" #define DEFAULT_THEME_NAME "Default"
#define TEST_SOUND_FILENAME "player_join" #define TEST_SOUND_FILENAME "player_join"
SoundEngine::SoundEngine(QObject *parent) : QObject(parent), player(0) SoundEngine::SoundEngine(QObject *parent) : QObject(parent), player(nullptr)
{ {
ensureThemeDirectoryExists(); ensureThemeDirectoryExists();
connect(&SettingsCache::instance(), SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot())); connect(&SettingsCache::instance(), SIGNAL(soundThemeChanged()), this, SLOT(themeChangedSlot()));
@ -30,7 +30,7 @@ SoundEngine::~SoundEngine()
void SoundEngine::soundEnabledChanged() void SoundEngine::soundEnabledChanged()
{ {
if (SettingsCache::instance().getSoundEnabled()) { if (SettingsCache::instance().getSoundEnabled()) {
qDebug("SoundEngine: enabling sound"); qDebug() << "SoundEngine: enabling sound with" << audioData.size() << "sounds";
if (!player) { if (!player) {
player = new QMediaPlayer; player = new QMediaPlayer;
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -39,11 +39,11 @@ void SoundEngine::soundEnabledChanged()
#endif #endif
} }
} else { } else {
qDebug("SoundEngine: disabling sound"); qDebug() << "SoundEngine: disabling sound";
if (player) { if (player) {
player->stop(); player->stop();
player->deleteLater(); player->deleteLater();
player = 0; player = nullptr;
} }
} }
} }
@ -54,28 +54,16 @@ void SoundEngine::playSound(QString fileName)
return; return;
} }
// still playing the previous sound?
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
if (player->playbackState() == QMediaPlayer::PlaybackState::PlayingState) {
#else
if (player->state() == QMediaPlayer::PlayingState) {
#endif
return;
}
if (!audioData.contains(fileName)) { if (!audioData.contains(fileName)) {
return; return;
} }
qDebug() << "playing" << fileName; player->stop();
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
player->audioOutput()->setVolume(SettingsCache::instance().getMasterVolume()); player->audioOutput()->setVolume(SettingsCache::instance().getMasterVolume());
player->stop();
player->setSource(QUrl::fromLocalFile(audioData[fileName])); player->setSource(QUrl::fromLocalFile(audioData[fileName]));
#else #else
player->setVolume(SettingsCache::instance().getMasterVolume()); player->setVolume(SettingsCache::instance().getMasterVolume());
player->stop();
player->setMedia(QUrl::fromLocalFile(audioData[fileName])); player->setMedia(QUrl::fromLocalFile(audioData[fileName]));
#endif #endif
player->play(); player->play();
@ -137,50 +125,29 @@ void SoundEngine::themeChangedSlot()
audioData.clear(); audioData.clear();
static const QStringList fileNames = QStringList() static const QStringList extensions = {".wav", ".mp3", ".ogg"};
// Phases static const QStringList fileNames = {
<< "untap_step" // Phases
<< "upkeep_step" "untap_step", "upkeep_step", "draw_step", "main_1", "start_combat", "attack_step", "block_step", "damage_step",
<< "draw_step" "end_combat", "main_2", "end_step",
<< "main_1" // Game Actions
<< "start_combat" "draw_card", "play_card", "tap_card", "untap_card", "shuffle", "roll_dice", "life_change",
<< "attack_step" // Player
<< "block_step" "player_join", "player_leave", "player_disconnect", "player_reconnect", "player_concede",
<< "damage_step" // Spectator
<< "end_combat" "spectator_join", "spectator_leave",
<< "main_2" // Buddy
<< "end_step" "buddy_join", "buddy_leave",
// Game Actions // Chat & UI
<< "draw_card" "chat_mention", "all_mention", "private_message"};
<< "play_card"
<< "tap_card"
<< "untap_card"
<< "shuffle"
<< "roll_dice"
<< "life_change"
// Player
<< "player_join"
<< "player_leave"
<< "player_disconnect"
<< "player_reconnect"
<< "player_concede"
// Spectator
<< "spectator_join"
<< "spectator_leave"
// Buddy
<< "buddy_join"
<< "buddy_leave"
// Chat & UI
<< "chat_mention"
<< "all_mention"
<< "private_message";
for (int i = 0; i < fileNames.size(); ++i) { for (const QString &extension : extensions) {
if (!dir.exists(fileNames[i] + ".wav")) for (const QString &name : fileNames) {
continue; QFile file(dir.filePath(name + extension));
if (file.exists()) {
QFile file(dir.filePath(fileNames[i] + ".wav")); audioData.insert(name, file.fileName());
audioData.insert(fileNames[i], file.fileName()); }
}
} }
soundEnabledChanged(); soundEnabledChanged();