Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sound playback when game is paused and rewound #44

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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