diff --git a/CHANGELOG.md b/CHANGELOG.md index 86eb30b..5742710 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +### 2.1.6 (17 Oct 2024) +- fixed a bug that caused an error when loading a sound more than twice. + ### 2.1.5 (11 Oct 2024) - added `readSamplesFrom*()` methods to read N audio data within a time range from a file or memory #75. Example in `example/lib/wave_data/wave_data.dart`. diff --git a/example/tests/tests.dart b/example/tests/tests.dart index ce82b03..7eab54c 100644 --- a/example/tests/tests.dart +++ b/example/tests/tests.dart @@ -868,6 +868,11 @@ Future testAsyncMultiLoad() async { ]; await Future.wait(sounds); + // loading the same asset many times should not throw and just + // display a warning. + await SoLoud.instance.loadAsset('assets/audio/tic-1.wav'); + await SoLoud.instance.loadAsset('assets/audio/tic-1.wav'); + await SoLoud.instance.loadAsset('assets/audio/tic-1.wav'); deinit(); return strBuf; diff --git a/lib/src/soloud.dart b/lib/src/soloud.dart index 60b19a1..b6a5d5c 100644 --- a/lib/src/soloud.dart +++ b/lib/src/soloud.dart @@ -437,35 +437,30 @@ interface class SoLoud { final completeFileName = result['completeFileName'] as String; final hash = result['hash'] as int; + final newSound = AudioSource(SoundHash(hash)); + final alreadyLoaded = _activeSounds + .where((sound) => sound.soundHash == newSound.soundHash) + .length == + 1; _logPlayerError(error, from: 'loadFile() result'); if (error == PlayerErrors.noError) { - final newSound = AudioSource(SoundHash(hash)); - _activeSounds.add(newSound); - loadedFileCompleters[result['completeFileName']]! - .complete(newSound); + if (!alreadyLoaded) { + _activeSounds.add(newSound); + } } else if (error == PlayerErrors.fileAlreadyLoaded) { - _log.warning(() => "Sound '$completeFileName' was already loaded. " - 'Prefer loading only once, and reusing the loaded sound ' - 'when playing.'); - final newSound = AudioSource(SoundHash(hash)); - final alreadyLoaded = _activeSounds - .where((sound) => sound.soundHash == newSound.soundHash) - .length == - 1; - assert( - alreadyLoaded, - 'Sound is already loaded but missing from _activeSounds. ' - 'This is probably a bug in flutter_soloud, please file.'); - // If we are here, the file has been already loaded but there is - // no corrispondence int the local list of sounds. Add it to - // the list safely because the cpp loadFile() compute the hash - // using the file name. - _activeSounds.add(newSound); - loadedFileCompleters[result['completeFileName']] - ?.complete(newSound); + // If we are here, the file has been already loaded on C++ side. + // Check if it is already in [_activeSounds], if not add it. + if (alreadyLoaded) { + _log.warning(() => "Sound '$completeFileName' was already " + 'loaded. Prefer loading only once, and reusing the loaded ' + 'sound when playing.'); + } else { + _activeSounds.add(newSound); + } } else { throw SoLoudCppException.fromPlayerError(error); } + loadedFileCompleters[result['completeFileName']]?.complete(newSound); } }); } diff --git a/pubspec.yaml b/pubspec.yaml index aff1a9e..9b58a3f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ description: >- A low-level audio plugin for Flutter, mainly meant for games and immersive apps. Based on the SoLoud (C++) audio engine. -version: 2.1.5 +version: 2.1.6 issue_tracker: https://github.com/alnitak/flutter_soloud/issues homepage: https://github.com/alnitak/flutter_soloud maintainer: Marco Bavagnoli (@lildeimos)