Skip to content

Commit

Permalink
Fix sound playback when game is paused and rewound (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m authored Feb 11, 2025
1 parent 7f421da commit 1b6b4e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,13 @@ class AssetStore {
}

assetConfig.sounds.forEach { sound ->
val soundFile = resourcesVfs[assetConfig.folder + "/" + sound.value].readMusic()
val soundChannel = soundFile.decode().toWav().readMusic().play() // -- convert to WAV
// val soundChannel = soundFile.play()
// val soundChannel = resourcesVfs[assetConfig.assetFolderName + "/" + sound.value].readSound().play()
val soundFile = resourcesVfs[assetConfig.folder + "/" + sound.value] .readSound( //readMusic(
props = AudioDecodingProps(exactTimings = true),
streaming = true
)
// val soundChannel = soundFile.decode().toWav().readMusic().play() // -- convert to WAV
val soundChannel = soundFile.play()
// val soundChannel2 = resourcesVfs[assetConfig.folder + "/" + sound.value].readSound().play()

soundChannel.pause()
sounds[sound.key] = Pair(type, soundChannel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ class SoundSystem : IteratingSystem(

val soundChannel = assetStore.getSound(soundComponent.name)

// Sound enabling/disabling triggered from outside
if (soundEnabled == soundEnabledNext) {
// Sound enabling/disabling triggered from outside

soundEnabledNext = !soundEnabled
if (soundEnabled) {
// if (soundComponent.isPlaying && soundChannel.paused) soundChannel.resume()
if (soundComponent.isPlaying) {
// soundChannel.current = 10000.milliseconds
// soundChannel.reset()
soundChannel.current = soundComponent.position.milliseconds
soundChannel.resume()
println("SoundSystem: Resume sound '${soundComponent.name}'")
println("SoundSystem: Resume sound '${soundComponent.name}' at position '${soundComponent.position}'")
}
} else {
// if(!soundChannel.paused) soundChannel.pause()
Expand All @@ -86,8 +89,8 @@ class SoundSystem : IteratingSystem(
}
}

// continuously save the play position
soundComponent.position = soundChannel.current.milliseconds
// continuously save the play position only when game is running - otherwise we overwrite the sound position
if (soundEnabled) soundComponent.position = soundChannel.current.milliseconds

// getOrNull(entity)?.let { channel ->
// sound.position = channel.current.milliseconds
Expand Down

0 comments on commit 1b6b4e9

Please sign in to comment.