feat: Implement audio shutdown procedure for graceful app exit

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-23 17:38:18 +01:00
parent f4d6db2db0
commit a7353e45b3
3 changed files with 95 additions and 17 deletions
@@ -26,6 +26,8 @@ class Wolf3d {
/// Shared engine audio backend used by menus and gameplay sessions.
final EngineAudio audio;
Future<void>? _audioShutdownFuture;
/// Engine menu background color as 24-bit RGB.
int menuBackgroundRgb = 0x890000;
@@ -246,6 +248,24 @@ class Wolf3d {
return this;
}
/// Stops and disposes shared audio exactly once for app shutdown.
///
/// Repeated calls return the same in-flight/completed future so hosts can
/// safely invoke shutdown from multiple lifecycle paths.
Future<void> shutdownAudio() {
final existing = _audioShutdownFuture;
if (existing != null) {
return existing;
}
final shutdown = () async {
await audio.stopAllAudio();
audio.dispose();
}();
_audioShutdownFuture = shutdown;
return shutdown;
}
/// Loads an asset from the Flutter bundle, returning `null` when absent.
Future<ByteData?> _tryLoad(String path) async {
try {