Fix city ambience sound not being stopped when the city screen is updated via UncivGame.replaceCurrentScreen

This commit is contained in:
Azzurite 2022-06-22 10:44:39 +02:00
parent 2b2c8c5d1b
commit 884a16d632
2 changed files with 23 additions and 7 deletions

View File

@ -4,15 +4,23 @@ import com.badlogic.gdx.Files
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.audio.Music import com.badlogic.gdx.audio.Music
import com.badlogic.gdx.files.FileHandle import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.utils.Disposable
import com.unciv.UncivGame import com.unciv.UncivGame
import com.unciv.logic.city.CityInfo import com.unciv.logic.city.CityInfo
import com.unciv.utils.Log 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 val soundsLocation = Files.FileType.Local
private var playingCitySound: Music? = null private var playingCitySound: Music? = null
val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats val fileExtensions = listOf("mp3", "ogg", "wav") // All Gdx formats
init {
play(city)
}
private fun getFile(path: String) = private fun getFile(path: String) =
if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable) if (soundsLocation == Files.FileType.External && Gdx.files.isExternalStorageAvailable)
Gdx.files.external(path) Gdx.files.external(path)
@ -36,7 +44,9 @@ class CityAmbiencePlayer {
.filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions } .filter { it.exists() && !it.isDirectory && it.extension() in fileExtensions }
.firstOrNull { it.name().contains(fileName) } .firstOrNull { it.name().contains(fileName) }
fun play(city: CityInfo) { private fun play(city: CityInfo) {
if (UncivGame.Current.settings.citySoundsVolume == 0f) return
if (playingCitySound != null) if (playingCitySound != null)
stop() stop()
try { try {
@ -51,7 +61,11 @@ class CityAmbiencePlayer {
} }
} }
fun stop() { private fun stop() {
playingCitySound?.dispose() playingCitySound?.dispose()
} }
override fun dispose() {
stop()
}
} }

View File

@ -85,7 +85,6 @@ class CityScreen(
keyShortcuts.add(KeyCharAndCode.BACK) keyShortcuts.add(KeyCharAndCode.BACK)
onActivation { onActivation {
exit() 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 // 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 nextTileToOwn = city.expansion.chooseNewTileToOwn()
private val cityAmbiencePlayer = CityAmbiencePlayer() private val cityAmbiencePlayer = CityAmbiencePlayer(city)
init { init {
if (city.isWeLoveTheKingDayActive() && UncivGame.Current.settings.citySoundsVolume > 0) { if (city.isWeLoveTheKingDayActive() && UncivGame.Current.settings.citySoundsVolume > 0) {
SoundPlayer.play(UncivSound("WLTK")) SoundPlayer.play(UncivSound("WLTK"))
} }
if (UncivGame.Current.settings.citySoundsVolume > 0)
cityAmbiencePlayer.play(city)
UncivGame.Current.settings.addCompletedTutorialTask("Enter city screen") UncivGame.Current.settings.addCompletedTutorialTask("Enter city screen")
@ -431,4 +428,9 @@ class CityScreen(
} }
override fun recreate(): BaseScreen = CityScreen(city) override fun recreate(): BaseScreen = CityScreen(city)
override fun dispose() {
cityAmbiencePlayer.dispose()
super.dispose()
}
} }