// =========================================================================== // 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; final AudioPlayer _sfxPlayer = AudioPlayer(); 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) { // Se abbiamo appena silenziato, FERMA TUTTO immediatamente. await _bgmPlayer.pause(); await _sfxPlayer.stop(); } else { // Se riaccendiamo, fai ripartire la canzone 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.wood: audioPath = 'audio/bgm/Legno_Canopy.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'; // <-- DEVI INSERIRE QUESTO FILE IN ASSETS 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: // Usiamo l'effetto arcade o cyber per la musica file = 'minimal_line.wav'; break; case AppThemeType.doodle: case AppThemeType.wood: file = 'doodle_line.wav'; break; case AppThemeType.cyberpunk: case AppThemeType.grimorio: file = 'cyber_line.wav'; break; } if (file.isNotEmpty) { try { await _sfxPlayer.play(AssetSource('audio/sfx/$file'), volume: 1.0); } catch (e) { debugPrint("Errore SFX Linea: $file"); } } } 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: case AppThemeType.wood: file = 'doodle_box.wav'; break; case AppThemeType.cyberpunk: case AppThemeType.grimorio: file = 'cyber_box.wav'; break; } if (file.isNotEmpty) { try { await _sfxPlayer.play(AssetSource('audio/sfx/$file'), volume: 1.0); } catch (e) { debugPrint("Errore SFX Box: $file"); } } } void playBonusSfx() async { if (isMuted) return; try { await _sfxPlayer.play(AssetSource('audio/sfx/bonus.wav'), volume: 1.0); } catch(e) {} } void playBombSfx() async { if (isMuted) return; try { await _sfxPlayer.play(AssetSource('audio/sfx/bomb.wav'), volume: 1.0); } catch(e) {} } }