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