Skip to content

Commit

Permalink
Merge pull request #139 from alnitak/multiple_load
Browse files Browse the repository at this point in the history
fix: loading same sound multiple times
  • Loading branch information
filiph authored Oct 17, 2024
2 parents fc8a7a8 + 94955a9 commit f32e5ee
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand Down
5 changes: 5 additions & 0 deletions example/tests/tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,11 @@ Future<StringBuffer> 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;
Expand Down
41 changes: 18 additions & 23 deletions lib/src/soloud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f32e5ee

Please sign in to comment.