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 = [ final loadables = [
...game.preLoadAssets(), ...game.preLoadAssets(),
pinballAudio.load(), ...pinballAudio.load(),
...BonusAnimation.loadAssets(), ...BonusAnimation.loadAssets(),
...SelectedCharacter.loadAssets(), ...SelectedCharacter.loadAssets(),
]; ];

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

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

Loading…
Cancel
Save