feat: improving pinball audio code and loading

pull/325/head
Erick Zanardo 3 years ago
parent 8a8f86ba56
commit 4cda232028

@ -53,7 +53,7 @@ class PinballGamePage extends StatelessWidget {
final loadables = [
...game.preLoadAssets(),
pinballAudio.load(),
...pinballAudio.load(),
...BonusAnimation.loadAssets(),
...SelectedCharacter.loadAssets(),
];

@ -71,26 +71,36 @@ class PinballAudio {
late AudioPool _bumperBPool;
/// Loads the sounds effects into the memory
Future<void> load() async {
List<Future<void>> load() {
_configureAudioCache(FlameAudio.audioCache);
_bumperAPool = await _createAudioPool(
_prefixFile(Assets.sfx.bumperA),
maxPlayers: 4,
prefix: '',
);
return [
_poolFactory(Assets.sfx.bumperA).then((pool) => _bumperAPool = pool),
_poolFactory(Assets.sfx.bumperB).then((pool) => _bumperBPool = pool),
_preCacheSingleAudio(_prefixFile(Assets.sfx.google)),
_preCacheSingleAudio(_prefixFile(Assets.sfx.ioPinballVoiceOver)),
_preCacheSingleAudio(_prefixFile(Assets.music.background)),
];
}
_bumperBPool = await _createAudioPool(
_prefixFile(Assets.sfx.bumperB),
maxPlayers: 4,
String _prefixFile(String file) {
return 'packages/pinball_audio/$file';
}
Future<AudioPool> _poolFactory(String file, {int maxPlayers = 4}) {
return _createAudioPool(
_prefixFile(file),
maxPlayers: maxPlayers,
prefix: '',
);
}
await Future.wait([
_preCacheSingleAudio(_prefixFile(Assets.sfx.google)),
_preCacheSingleAudio(_prefixFile(Assets.sfx.ioPinballVoiceOver)),
_preCacheSingleAudio(_prefixFile(Assets.music.background)),
]);
void _play(String file) {
_playSingleAudio(_prefixFile(file));
}
void _loop(String file) {
_loopSingleAudio(_prefixFile(file));
}
/// Plays a random bumper sfx.
@ -99,21 +109,11 @@ class PinballAudio {
}
/// Plays the google word bonus
void googleBonus() {
_playSingleAudio(_prefixFile(Assets.sfx.google));
}
void googleBonus() => _play(Assets.sfx.google);
/// Plays the I/O Pinball voice over audio.
void ioPinballVoiceOver() {
_playSingleAudio(_prefixFile(Assets.sfx.ioPinballVoiceOver));
}
void ioPinballVoiceOver() => _play(Assets.sfx.ioPinballVoiceOver);
/// Plays the background music
void backgroundMusic() {
_loopSingleAudio(_prefixFile(Assets.music.background));
}
String _prefixFile(String file) {
return 'packages/pinball_audio/$file';
}
void backgroundMusic() => _loop(Assets.music.background);
}

@ -97,7 +97,7 @@ void main() {
group('load', () {
test('creates the bumpers pools', () async {
await audio.load();
await Future.wait(audio.load());
verify(
() => createAudioPool.onCall(
@ -117,7 +117,7 @@ void main() {
});
test('configures the audio cache instance', () async {
await audio.load();
await Future.wait(audio.load());
verify(() => configureAudioCache.onCall(FlameAudio.audioCache))
.called(1);
@ -129,13 +129,13 @@ void main() {
playSingleAudio: playSingleAudio.onCall,
preCacheSingleAudio: preCacheSingleAudio.onCall,
);
await audio.load();
await Future.wait(audio.load());
expect(FlameAudio.audioCache.prefix, equals(''));
});
test('pre cache the assets', () async {
await audio.load();
await Future.wait(audio.load());
verify(
() => preCacheSingleAudio
@ -184,7 +184,7 @@ void main() {
group('when seed is true', () {
test('plays the bumper A sound pool', () async {
when(seed.nextBool).thenReturn(true);
await audio.load();
await Future.wait(audio.load());
audio.bumper();
verify(() => bumperAPool.start(volume: 0.6)).called(1);
@ -194,7 +194,7 @@ void main() {
group('when seed is false', () {
test('plays the bumper B sound pool', () async {
when(seed.nextBool).thenReturn(false);
await audio.load();
await Future.wait(audio.load());
audio.bumper();
verify(() => bumperBPool.start(volume: 0.6)).called(1);
@ -204,7 +204,7 @@ void main() {
group('googleBonus', () {
test('plays the correct file', () async {
await audio.load();
await Future.wait(audio.load());
audio.googleBonus();
verify(
@ -216,7 +216,7 @@ void main() {
group('ioPinballVoiceOver', () {
test('plays the correct file', () async {
await audio.load();
await Future.wait(audio.load());
audio.ioPinballVoiceOver();
verify(
@ -229,7 +229,7 @@ void main() {
group('backgroundMusic', () {
test('plays the correct file', () async {
await audio.load();
await Future.wait(audio.load());
audio.backgroundMusic();
verify(

Loading…
Cancel
Save