Fix android pause (#10318)

* Fix MusicController Pause handling for Android's pause event

* Revert inadvertent instrumentation change
This commit is contained in:
SomeTroglodyte
2023-10-16 08:39:20 +02:00
committed by GitHub
parent 9efefcce27
commit 482900f07b
2 changed files with 18 additions and 17 deletions

View File

@ -446,9 +446,11 @@ open class UncivGame(val isConsoleMode: Boolean = false) : Game(), PlatformSpeci
} }
override fun pause() { override fun pause() {
// Needs to go ASAP - on Android, there's a tiny race condition: The OS will stop our playback forcibly, it likely
// already has, but if we do _our_ pause before the MusicController timer notices, it will at least remember the current track.
if (::musicController.isInitialized) musicController.pause()
val curGameInfo = gameInfo val curGameInfo = gameInfo
if (curGameInfo != null) files.requestAutoSave(curGameInfo) if (curGameInfo != null) files.requestAutoSave(curGameInfo)
if (::musicController.isInitialized) musicController.pause()
super.pause() super.pause()
} }

View File

@ -121,17 +121,17 @@ class MusicController {
private var musicTimer: Timer? = null private var musicTimer: Timer? = null
private enum class ControllerState { private enum class ControllerState(val canPause: Boolean = false) {
/** Own timer stopped, if using the HardenedGdxAudio callback just do nothing */ /** Own timer stopped, if using the HardenedGdxAudio callback just do nothing */
Idle, Idle,
/** Loop will release everything and go [Idle] if it encounters this state. */ /** Loop will release everything and go [Idle] if it encounters this state. */
Cleanup, Cleanup,
/** Play a track to its end, then silence for a while, then choose another track */ /** Play a track to its end, then silence for a while, then choose another track */
Playing, Playing(true),
/** Play a track to its end, then [Cleanup] */ /** Play a track to its end, then [Cleanup] */
PlaySingle, PlaySingle(true),
/** Wait for a while in silence to start next track */ /** Wait for a while in silence to start next track */
Silence, Silence(true),
/** Music fades to pause or is paused. Continue with chooseTrack or resume. */ /** Music fades to pause or is paused. Continue with chooseTrack or resume. */
Pause, Pause,
/** Fade out then [Cleanup] */ /** Fade out then [Cleanup] */
@ -166,12 +166,10 @@ class MusicController {
audioExceptionHandler(ex, music) audioExceptionHandler(ex, music)
} }
/** @return the path of the playing track or null if none playing */ /** @return the path of the playing track or empty string if none playing */
private fun currentlyPlaying(): String = when(state) { private fun currentlyPlaying(): String =
ControllerState.Playing, ControllerState.PlaySingle, ControllerState.Pause -> if (state.canPause) musicHistory.lastOrNull() ?: ""
musicHistory.lastOrNull() ?: "" else ""
else -> ""
}
/** Registers a callback that will be called with the new track every time it changes. /** Registers a callback that will be called with the new track every time it changes.
* *
@ -263,7 +261,7 @@ class MusicController {
} }
} // else wait for the thread of next.load() to finish } // else wait for the thread of next.load() to finish
} else if (!current!!.isPlaying()) { } else if (!current!!.isPlaying()) {
// normal end of track // normal end of track - or the OS stopped the playback (Android pause)
clearCurrent() clearCurrent()
// rest handled next tick // rest handled next tick
} else { } else {
@ -522,13 +520,14 @@ class MusicController {
*/ */
fun pause(speedFactor: Float = 1f) { fun pause(speedFactor: Float = 1f) {
Log.debug("MusicTrackController.pause called") Log.debug("MusicTrackController.pause called")
val controller = current ?: return
if (state != ControllerState.Playing && state != ControllerState.PlaySingle) return if (!state.canPause) return
state = ControllerState.Pause
val fadingStep = defaultFadingStep * speedFactor.coerceIn(0.001f..1000f) val fadingStep = defaultFadingStep * speedFactor.coerceIn(0.001f..1000f)
controller.startFade(MusicTrackController.State.FadeOut, fadingStep) current?.startFade(MusicTrackController.State.FadeOut, fadingStep)
if (next?.state == MusicTrackController.State.FadeIn) if (next?.state == MusicTrackController.State.FadeIn)
next!!.startFade(MusicTrackController.State.FadeOut) next!!.startFade(MusicTrackController.State.FadeOut)
state = ControllerState.Pause
} }
/** /**
@ -546,7 +545,7 @@ class MusicController {
// currently only the main menu resumes, and then it's perfect: // currently only the main menu resumes, and then it's perfect:
state = ControllerState.Playing state = ControllerState.Playing
current!!.play() current!!.play()
} else if (state == ControllerState.Cleanup) { } else if (state == ControllerState.Cleanup || state == ControllerState.Pause) {
chooseTrack() chooseTrack()
} }
} }