| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import 'package:just_audio/just_audio.dart';
- import 'package:flutter/services.dart';
- import 'package:vibration/vibration.dart';
- class AudioManager {
- static final AudioManager _instance = AudioManager._internal();
- factory AudioManager() => _instance;
- AudioManager._internal();
- final AudioPlayer _backgroundPlayer = AudioPlayer();
- final AudioPlayer _effectPlayer = AudioPlayer();
-
- bool _soundEnabled = true;
- bool _musicEnabled = true;
- bool _hapticEnabled = true;
- bool get soundEnabled => _soundEnabled;
- bool get musicEnabled => _musicEnabled;
- bool get hapticEnabled => _hapticEnabled;
- Future<void> initialize() async {
- try {
- // Set up background music loop
- await _backgroundPlayer.setLoopMode(LoopMode.one);
-
- // For now, we'll use system sounds. In production, we'd load actual audio files
- // await _backgroundPlayer.setAsset('assets/audio/background_ambient.mp3');
- } catch (e) {
- print('Audio initialization failed: $e');
- }
- }
- Future<void> playBubblePop() async {
- if (!_soundEnabled) return;
-
- try {
- // Play system click sound as placeholder
- await SystemSound.play(SystemSoundType.click);
-
- // Add haptic feedback
- if (_hapticEnabled) {
- await _playHapticFeedback();
- }
- } catch (e) {
- print('Failed to play bubble pop sound: $e');
- }
- }
- Future<void> playBackgroundMusic() async {
- if (!_musicEnabled) return;
-
- try {
- // For now, we'll skip background music until we have audio files
- // await _backgroundPlayer.play();
- print('Background music would play here');
- } catch (e) {
- print('Failed to play background music: $e');
- }
- }
- Future<void> stopBackgroundMusic() async {
- try {
- await _backgroundPlayer.stop();
- } catch (e) {
- print('Failed to stop background music: $e');
- }
- }
- Future<void> pauseBackgroundMusic() async {
- try {
- await _backgroundPlayer.pause();
- } catch (e) {
- print('Failed to pause background music: $e');
- }
- }
- Future<void> resumeBackgroundMusic() async {
- if (!_musicEnabled) return;
-
- try {
- await _backgroundPlayer.play();
- } catch (e) {
- print('Failed to resume background music: $e');
- }
- }
- Future<void> _playHapticFeedback() async {
- try {
- if (await Vibration.hasVibrator() ?? false) {
- await Vibration.vibrate(duration: 50);
- }
- } catch (e) {
- print('Failed to play haptic feedback: $e');
- }
- }
- void setSoundEnabled(bool enabled) {
- _soundEnabled = enabled;
- }
- void setMusicEnabled(bool enabled) {
- _musicEnabled = enabled;
- if (!enabled) {
- stopBackgroundMusic();
- } else {
- playBackgroundMusic();
- }
- }
- void setHapticEnabled(bool enabled) {
- _hapticEnabled = enabled;
- }
- Future<void> dispose() async {
- await _backgroundPlayer.dispose();
- await _effectPlayer.dispose();
- }
- }
|