From 884a16d632ed0e1e930ba884c085e36e020978ba Mon Sep 17 00:00:00 2001 From: Azzurite Date: Wed, 22 Jun 2022 10:44:39 +0200 Subject: [PATCH] Fix city ambience sound not being stopped when the city screen is updated via UncivGame.replaceCurrentScreen --- .../com/unciv/ui/audio/CityAmbiencePlayer.kt | 20 ++++++++++++++++--- .../src/com/unciv/ui/cityscreen/CityScreen.kt | 10 ++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/ui/audio/CityAmbiencePlayer.kt b/core/src/com/unciv/ui/audio/CityAmbiencePlayer.kt index f300a2ff41..52dc3449c9 100644 --- a/core/src/com/unciv/ui/audio/CityAmbiencePlayer.kt +++ b/core/src/com/unciv/ui/audio/CityAmbiencePlayer.kt @@ -4,15 +4,23 @@ import com.badlogic.gdx.Files import com.badlogic.gdx.Gdx import com.badlogic.gdx.audio.Music import com.badlogic.gdx.files.FileHandle +import com.badlogic.gdx.utils.Disposable import com.unciv.UncivGame import com.unciv.logic.city.CityInfo import com.unciv.utils.Log -class CityAmbiencePlayer { +/** Must be [disposed][dispose]. Starts playing an ambience sound for the city when created. Stops playing the ambience sound when [disposed][dispose]. */ +class CityAmbiencePlayer( + city: CityInfo +) : Disposable { private val soundsLocation = Files.FileType.Local private var playingCitySound: Music? = null val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats + init { + play(city) + } + private fun getFile(path: String) = if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable) Gdx.files.external(path) @@ -36,7 +44,9 @@ class CityAmbiencePlayer { .filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions } .firstOrNull { it.name().contains(fileName) } - fun play(city: CityInfo) { + private fun play(city: CityInfo) { + if (UncivGame.Current.settings.citySoundsVolume == 0f) return + if (playingCitySound != null) stop() try { @@ -51,7 +61,11 @@ class CityAmbiencePlayer { } } - fun stop() { + private fun stop() { playingCitySound?.dispose() } + + override fun dispose() { + stop() + } } diff --git a/core/src/com/unciv/ui/cityscreen/CityScreen.kt b/core/src/com/unciv/ui/cityscreen/CityScreen.kt index 8c7b204611..0850bc5b81 100644 --- a/core/src/com/unciv/ui/cityscreen/CityScreen.kt +++ b/core/src/com/unciv/ui/cityscreen/CityScreen.kt @@ -85,7 +85,6 @@ class CityScreen( keyShortcuts.add(KeyCharAndCode.BACK) onActivation { exit() - cityAmbiencePlayer.stop() } } @@ -116,14 +115,12 @@ class CityScreen( // val should be OK as buying tiles is what changes this, and that would re-create the whole CityScreen private val nextTileToOwn = city.expansion.chooseNewTileToOwn() - private val cityAmbiencePlayer = CityAmbiencePlayer() + private val cityAmbiencePlayer = CityAmbiencePlayer(city) init { if (city.isWeLoveTheKingDayActive() && UncivGame.Current.settings.citySoundsVolume > 0) { SoundPlayer.play(UncivSound("WLTK")) } - if (UncivGame.Current.settings.citySoundsVolume > 0) - cityAmbiencePlayer.play(city) UncivGame.Current.settings.addCompletedTutorialTask("Enter city screen") @@ -431,4 +428,9 @@ class CityScreen( } override fun recreate(): BaseScreen = CityScreen(city) + + override fun dispose() { + cityAmbiencePlayer.dispose() + super.dispose() + } }