From 4cda232028302bf7429e45e916e3b89e5c1a96fc Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 3 May 2022 21:45:20 -0300 Subject: [PATCH] feat: improving pinball audio code and loading --- lib/game/view/pinball_game_page.dart | 2 +- .../pinball_audio/lib/src/pinball_audio.dart | 54 +++++++++---------- .../test/src/pinball_audio_test.dart | 18 +++---- 3 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/game/view/pinball_game_page.dart b/lib/game/view/pinball_game_page.dart index 6c823a69..2e97088f 100644 --- a/lib/game/view/pinball_game_page.dart +++ b/lib/game/view/pinball_game_page.dart @@ -53,7 +53,7 @@ class PinballGamePage extends StatelessWidget { final loadables = [ ...game.preLoadAssets(), - pinballAudio.load(), + ...pinballAudio.load(), ...BonusAnimation.loadAssets(), ...SelectedCharacter.loadAssets(), ]; diff --git a/packages/pinball_audio/lib/src/pinball_audio.dart b/packages/pinball_audio/lib/src/pinball_audio.dart index 07257fea..8e7d4b3b 100644 --- a/packages/pinball_audio/lib/src/pinball_audio.dart +++ b/packages/pinball_audio/lib/src/pinball_audio.dart @@ -71,26 +71,36 @@ class PinballAudio { late AudioPool _bumperBPool; /// Loads the sounds effects into the memory - Future load() async { + List> 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 _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); } diff --git a/packages/pinball_audio/test/src/pinball_audio_test.dart b/packages/pinball_audio/test/src/pinball_audio_test.dart index 916d0f34..eb18728d 100644 --- a/packages/pinball_audio/test/src/pinball_audio_test.dart +++ b/packages/pinball_audio/test/src/pinball_audio_test.dart @@ -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(