Skip to content

Commit b74b194

Browse files
committed
feat: Add audio assets and pre-caching
This commit introduces audio functionality to the game by integrating the `flame_audio` package. - **Initialization**: The `FlameAudio` engine is initialized at the start of the application. - **Asset Pre-caching**: All necessary audio files, including background music (`background.mp3`) and sound effects (`collide-bat.wav`, `collide-brick.wav`, `win.wav`, `game-over.wav`), are pre-loaded into the audio cache on app startup to ensure they are ready for playback without delay.
1 parent 6c6862d commit b74b194

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

lib/main.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import 'package:flutter/material.dart';
22
import 'package:hive_flutter/hive_flutter.dart';
3+
import 'package:flame_audio/flame_audio.dart';
34

45
import 'src/widgets/game_app.dart';
56

67
void main() async {
78
WidgetsFlutterBinding.ensureInitialized();
9+
FlameAudio.bgm.initialize();
10+
FlameAudio.audioCache.loadAll([
11+
"background.mp3",
12+
"collide-bat.wav",
13+
"collide-brick.wav",
14+
"win.wav",
15+
"game-over.wav",
16+
]);
817

918
await Hive.initFlutter();
1019
await Hive.openBox('bricked');

lib/src/brick_breaker.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class BrickBreaker extends FlameGame
6464
highScore.value = Hive.box('bricked').get('highScore', defaultValue: 0);
6565

6666
playState = PlayState.welcome;
67-
FlameAudio.bgm.initialize();
68-
6967
}
7068

7169
void startGame() {
@@ -78,7 +76,7 @@ class BrickBreaker extends FlameGame
7876
playState = PlayState.playing;
7977
score.value = 0;
8078
newHighScoreAchieved = false;
81-
FlameAudio.bgm.play("background.mp3");
79+
FlameAudio.bgm.play("background.mp3", volume: 0.7);
8280
world.add(
8381
Ball(
8482
difficultyModifier: difficultyModifier,

lib/src/components/ball.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class Ball extends CircleComponent
5656
},
5757
),
5858
);
59+
FlameAudio.bgm.stop();
5960
FlameAudio.play("game-over.wav");
6061
}
6162
} else if (other is Bat) {

0 commit comments

Comments
 (0)