From 8ef84d785ebaca5fb584d0b71521a07c1bea0a76 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Fri, 24 Sep 2021 09:00:46 +0200 Subject: [PATCH] Music controller - Fix pause (#5307) --- core/src/com/unciv/UncivGame.kt | 2 +- core/src/com/unciv/ui/audio/MusicController.kt | 6 +++++- core/src/com/unciv/ui/audio/MusicTrackController.kt | 2 ++ core/src/com/unciv/ui/worldscreen/WorldScreen.kt | 3 +++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/UncivGame.kt b/core/src/com/unciv/UncivGame.kt index 3f3386f556..b94a873d45 100644 --- a/core/src/com/unciv/UncivGame.kt +++ b/core/src/com/unciv/UncivGame.kt @@ -167,7 +167,7 @@ class UncivGame(parameters: UncivGameParameters) : Game() { override fun dispose() { cancelDiscordEvent?.invoke() Sounds.clearCache() - if (::musicController.isInitialized) musicController.shutdown() + if (::musicController.isInitialized) musicController.gracefulShutdown() // Do allow fade-out // Log still running threads (on desktop that should be only this one and "DestroyJavaVM") val numThreads = Thread.activeCount() diff --git a/core/src/com/unciv/ui/audio/MusicController.kt b/core/src/com/unciv/ui/audio/MusicController.kt index f6d64dd0e7..eba5b60705 100644 --- a/core/src/com/unciv/ui/audio/MusicController.kt +++ b/core/src/com/unciv/ui/audio/MusicController.kt @@ -193,7 +193,7 @@ class MusicController { /** * Chooses and plays a music track using an adaptable approach - for details see the wiki. * Called without parameters it will choose a new ambient music track and start playing it with fade-in/out. - * Will do nothing when no music files exist. + * Will do nothing when no music files exist or the master volume is zero. * * @param prefix file name prefix, meant to represent **Context** - in most cases a Civ name or default "Ambient" * @param suffix file name suffix, meant to represent **Mood** - e.g. Peace, War, Theme... @@ -205,6 +205,8 @@ class MusicController { suffix: String = "", flags: EnumSet = EnumSet.noneOf(MusicTrackChooserFlags::class.java) ): Boolean { + if (baseVolume == 0f) return false + val musicFile = chooseFile(prefix, suffix, flags) if (musicFile == null) { @@ -265,6 +267,8 @@ class MusicController { if ((state != ControllerState.Playing && state != ControllerState.PlaySingle) || current == null) return val fadingStep = defaultFadingStep * speedFactor.coerceIn(0.001f..1000f) current!!.startFade(MusicTrackController.State.FadeOut, fadingStep) + if (next?.state == MusicTrackController.State.FadeIn) + next!!.startFade(MusicTrackController.State.FadeOut) state = ControllerState.Pause } diff --git a/core/src/com/unciv/ui/audio/MusicTrackController.kt b/core/src/com/unciv/ui/audio/MusicTrackController.kt index b7d5aafe7b..8dcc25d809 100644 --- a/core/src/com/unciv/ui/audio/MusicTrackController.kt +++ b/core/src/com/unciv/ui/audio/MusicTrackController.kt @@ -105,6 +105,8 @@ class MusicTrackController(private var volume: Float) { return } fadeVolume = 0f + music!!.volume = 0f + music!!.pause() state = State.Idle } diff --git a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt index 35a3631a7a..adfead82b7 100644 --- a/core/src/com/unciv/ui/worldscreen/WorldScreen.kt +++ b/core/src/com/unciv/ui/worldscreen/WorldScreen.kt @@ -108,6 +108,9 @@ class WorldScreen(val gameInfo: GameInfo, val viewingCiv:CivilizationInfo) : Cam ToastPopup("Not enough memory on phone to load game!", this) } + // resume music (in case choices from the menu lead to instantiation of a new WorldScreen) + UncivGame.Current.musicController.resume() + techButtonHolder.touchable = Touchable.enabled techButtonHolder.onClick(UncivSound.Paper) { game.setScreen(TechPickerScreen(viewingCiv))