tetraq/lib/services/audio_service.dart

145 lines
4.1 KiB
Dart
Raw Normal View History

2026-03-01 20:59:06 +01:00
// ===========================================================================
// FILE: lib/services/audio_service.dart
// ===========================================================================
2026-02-27 23:35:54 +01:00
import 'package:flutter/material.dart';
import 'package:audioplayers/audioplayers.dart';
import '../core/app_colors.dart';
2026-03-04 21:00:00 +01:00
import 'package:shared_preferences/shared_preferences.dart';
2026-02-27 23:35:54 +01:00
class AudioService extends ChangeNotifier {
static final AudioService instance = AudioService._internal();
AudioService._internal();
bool isMuted = false;
2026-03-23 01:00:01 +01:00
// Abbiamo rimosso _sfxPlayer perché ora ogni suono crea un player usa e getta
2026-03-04 21:00:00 +01:00
final AudioPlayer _bgmPlayer = AudioPlayer();
2026-02-27 23:35:54 +01:00
2026-03-13 22:00:00 +01:00
AppThemeType _currentTheme = AppThemeType.doodle;
2026-03-11 22:00:01 +01:00
2026-03-04 21:00:00 +01:00
Future<void> init() async {
final prefs = await SharedPreferences.getInstance();
isMuted = prefs.getBool('isMuted') ?? false;
await _bgmPlayer.setReleaseMode(ReleaseMode.loop);
}
void toggleMute() async {
2026-02-27 23:35:54 +01:00
isMuted = !isMuted;
2026-03-04 21:00:00 +01:00
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isMuted', isMuted);
if (isMuted) {
2026-03-11 22:00:01 +01:00
await _bgmPlayer.pause();
2026-03-04 21:00:00 +01:00
} else {
2026-03-11 22:00:01 +01:00
playBgm(_currentTheme);
2026-03-04 21:00:00 +01:00
}
2026-02-27 23:35:54 +01:00
notifyListeners();
}
2026-03-04 21:00:00 +01:00
Future<void> playBgm(AppThemeType theme) async {
2026-03-13 22:00:00 +01:00
_currentTheme = theme;
2026-03-11 22:00:01 +01:00
await _bgmPlayer.stop();
2026-03-04 21:00:00 +01:00
if (isMuted) return;
String audioPath = '';
switch (theme) {
case AppThemeType.cyberpunk:
audioPath = 'audio/bgm/Cyber_Dystopia.mp3';
break;
case AppThemeType.doodle:
audioPath = 'audio/bgm/Quad_Dreams.mp3';
break;
case AppThemeType.arcade:
audioPath = 'audio/bgm/8-bit_Prowler.mp3';
break;
case AppThemeType.grimorio:
audioPath = 'audio/bgm/Grimorio_Astral.mp3';
break;
2026-03-13 23:00:01 +01:00
case AppThemeType.music:
2026-03-15 17:00:01 +01:00
audioPath = 'audio/bgm/Music_Loop.mp3';
2026-03-13 23:00:01 +01:00
break;
2026-03-04 21:00:00 +01:00
}
if (audioPath.isNotEmpty) {
try {
2026-03-11 22:00:01 +01:00
await _bgmPlayer.play(AssetSource(audioPath), volume: 0.15);
2026-03-04 21:00:00 +01:00
} catch (e) {
debugPrint("Errore riproduzione BGM: $e");
}
}
}
Future<void> stopBgm() async {
await _bgmPlayer.stop();
}
2026-02-27 23:35:54 +01:00
void playLineSfx(AppThemeType theme) async {
if (isMuted) return;
String file = '';
switch (theme) {
2026-03-04 21:00:00 +01:00
case AppThemeType.arcade:
2026-03-15 17:00:01 +01:00
case AppThemeType.music:
2026-03-01 20:59:06 +01:00
file = 'minimal_line.wav'; break;
2026-02-27 23:35:54 +01:00
case AppThemeType.doodle:
file = 'doodle_line.wav'; break;
2026-03-01 20:59:06 +01:00
case AppThemeType.cyberpunk:
2026-03-04 21:00:00 +01:00
case AppThemeType.grimorio:
2026-03-01 20:59:06 +01:00
file = 'cyber_line.wav'; break;
2026-02-27 23:35:54 +01:00
}
2026-03-04 21:00:00 +01:00
if (file.isNotEmpty) {
try {
2026-03-23 01:00:01 +01:00
final player = AudioPlayer(); // Player dedicato
await player.play(AssetSource('audio/sfx/$file'), volume: 1.0);
player.onPlayerComplete.listen((_) => player.dispose());
2026-03-04 21:00:00 +01:00
} catch (e) {
2026-03-23 01:00:01 +01:00
debugPrint("Errore SFX Linea: $e");
2026-03-04 21:00:00 +01:00
}
}
2026-02-27 23:35:54 +01:00
}
void playBoxSfx(AppThemeType theme) async {
if (isMuted) return;
String file = '';
switch (theme) {
2026-03-01 20:59:06 +01:00
case AppThemeType.arcade:
2026-03-13 23:00:01 +01:00
case AppThemeType.music:
2026-03-01 20:59:06 +01:00
file = 'minimal_box.wav'; break;
2026-02-27 23:35:54 +01:00
case AppThemeType.doodle:
file = 'doodle_box.wav'; break;
2026-03-01 20:59:06 +01:00
case AppThemeType.cyberpunk:
case AppThemeType.grimorio:
file = 'cyber_box.wav'; break;
2026-02-27 23:35:54 +01:00
}
2026-03-04 21:00:00 +01:00
if (file.isNotEmpty) {
try {
2026-03-23 01:00:01 +01:00
final player = AudioPlayer(); // Player dedicato
await player.play(AssetSource('audio/sfx/$file'), volume: 1.0);
player.onPlayerComplete.listen((_) => player.dispose());
2026-03-04 21:00:00 +01:00
} catch (e) {
2026-03-23 01:00:01 +01:00
debugPrint("Errore SFX Box: $e");
2026-03-04 21:00:00 +01:00
}
}
2026-02-27 23:35:54 +01:00
}
void playBonusSfx() async {
if (isMuted) return;
2026-03-04 21:00:00 +01:00
try {
2026-03-23 01:00:01 +01:00
final player = AudioPlayer(); // Player dedicato
await player.play(AssetSource('audio/sfx/bonus.wav'), volume: 1.0);
player.onPlayerComplete.listen((_) => player.dispose());
2026-03-04 21:00:00 +01:00
} catch(e) {}
2026-02-27 23:35:54 +01:00
}
void playBombSfx() async {
if (isMuted) return;
2026-03-04 21:00:00 +01:00
try {
2026-03-23 01:00:01 +01:00
final player = AudioPlayer(); // Player dedicato
await player.play(AssetSource('audio/sfx/bomb.wav'), volume: 1.0);
player.onPlayerComplete.listen((_) => player.dispose());
2026-03-04 21:00:00 +01:00
} catch(e) {}
2026-02-27 23:35:54 +01:00
}
}