76 lines
1.9 KiB
Dart
76 lines
1.9 KiB
Dart
import 'package:audioplayers/audioplayers.dart';
|
|
|
|
class AudioManager {
|
|
static final AudioPlayer _backgroundMusic = AudioPlayer();
|
|
static final AudioPlayer _effectPlayer = AudioPlayer();
|
|
static bool _isMusicEnabled = true;
|
|
static bool _isSoundEnabled = true;
|
|
|
|
static final AudioManager _instance = AudioManager._internal();
|
|
static AudioManager get instance => _instance;
|
|
|
|
AudioManager._internal();
|
|
|
|
static Future<void> initAudio() async {
|
|
await _backgroundMusic.setReleaseMode(ReleaseMode.loop); // Musik akan diulang
|
|
await instance.startBackgroundMusic();
|
|
}
|
|
|
|
Future<void> startBackgroundMusic() async {
|
|
await _backgroundMusic.play(AssetSource('music/music.mp3'));
|
|
await _backgroundMusic.setReleaseMode(ReleaseMode.loop);
|
|
}
|
|
|
|
static Future<void> stopBackgroundMusic() async {
|
|
await _backgroundMusic.stop();
|
|
}
|
|
|
|
static Future<void> playClickSound() async {
|
|
if (_isSoundEnabled) {
|
|
await _effectPlayer.play(AssetSource('music/game.mp3'));
|
|
}
|
|
}
|
|
|
|
static Future<void> successsound() async {
|
|
if (_isSoundEnabled) {
|
|
await _effectPlayer.play(AssetSource('music/success.mp3'));
|
|
}
|
|
}
|
|
|
|
static Future<void> winsound() async {
|
|
if (_isSoundEnabled) {
|
|
await _effectPlayer.play(AssetSource('music/win.mp3'));
|
|
}
|
|
}
|
|
|
|
static Future<void> losesound() async {
|
|
if (_isSoundEnabled) {
|
|
await _effectPlayer.play(AssetSource('music/lose.mp3'));
|
|
}
|
|
}
|
|
|
|
void setSoundEnabled(bool enabled) {
|
|
_isSoundEnabled = enabled;
|
|
}
|
|
|
|
static void toggleMusic() {
|
|
_isMusicEnabled = !_isMusicEnabled;
|
|
if (_isMusicEnabled) {
|
|
instance.startBackgroundMusic();
|
|
} else {
|
|
stopBackgroundMusic();
|
|
}
|
|
}
|
|
|
|
static void toggleSound() {
|
|
_isSoundEnabled = !_isSoundEnabled;
|
|
}
|
|
|
|
static bool get isMusicEnabled => _isMusicEnabled;
|
|
static bool get isSoundEnabled => _isSoundEnabled;
|
|
|
|
Future<void> init() async {
|
|
// Initialize audio settings here
|
|
}
|
|
}
|