audio_manager.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'package:just_audio/just_audio.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:vibration/vibration.dart';
  4. class AudioManager {
  5. static final AudioManager _instance = AudioManager._internal();
  6. factory AudioManager() => _instance;
  7. AudioManager._internal();
  8. final AudioPlayer _backgroundPlayer = AudioPlayer();
  9. final AudioPlayer _effectPlayer = AudioPlayer();
  10. bool _soundEnabled = true;
  11. bool _musicEnabled = true;
  12. bool _hapticEnabled = true;
  13. bool get soundEnabled => _soundEnabled;
  14. bool get musicEnabled => _musicEnabled;
  15. bool get hapticEnabled => _hapticEnabled;
  16. Future<void> initialize() async {
  17. try {
  18. // Set up background music loop
  19. await _backgroundPlayer.setLoopMode(LoopMode.one);
  20. // For now, we'll use system sounds. In production, we'd load actual audio files
  21. // await _backgroundPlayer.setAsset('assets/audio/background_ambient.mp3');
  22. } catch (e) {
  23. print('Audio initialization failed: $e');
  24. }
  25. }
  26. Future<void> playBubblePop() async {
  27. if (!_soundEnabled) return;
  28. try {
  29. // Play system click sound as placeholder
  30. await SystemSound.play(SystemSoundType.click);
  31. // Add haptic feedback
  32. if (_hapticEnabled) {
  33. await _playHapticFeedback();
  34. }
  35. } catch (e) {
  36. print('Failed to play bubble pop sound: $e');
  37. }
  38. }
  39. Future<void> playBackgroundMusic() async {
  40. if (!_musicEnabled) return;
  41. try {
  42. // For now, we'll skip background music until we have audio files
  43. // await _backgroundPlayer.play();
  44. print('Background music would play here');
  45. } catch (e) {
  46. print('Failed to play background music: $e');
  47. }
  48. }
  49. Future<void> stopBackgroundMusic() async {
  50. try {
  51. await _backgroundPlayer.stop();
  52. } catch (e) {
  53. print('Failed to stop background music: $e');
  54. }
  55. }
  56. Future<void> pauseBackgroundMusic() async {
  57. try {
  58. await _backgroundPlayer.pause();
  59. } catch (e) {
  60. print('Failed to pause background music: $e');
  61. }
  62. }
  63. Future<void> resumeBackgroundMusic() async {
  64. if (!_musicEnabled) return;
  65. try {
  66. await _backgroundPlayer.play();
  67. } catch (e) {
  68. print('Failed to resume background music: $e');
  69. }
  70. }
  71. Future<void> _playHapticFeedback() async {
  72. try {
  73. if (await Vibration.hasVibrator() ?? false) {
  74. await Vibration.vibrate(duration: 50);
  75. }
  76. } catch (e) {
  77. print('Failed to play haptic feedback: $e');
  78. }
  79. }
  80. void setSoundEnabled(bool enabled) {
  81. _soundEnabled = enabled;
  82. }
  83. void setMusicEnabled(bool enabled) {
  84. _musicEnabled = enabled;
  85. if (!enabled) {
  86. stopBackgroundMusic();
  87. } else {
  88. playBackgroundMusic();
  89. }
  90. }
  91. void setHapticEnabled(bool enabled) {
  92. _hapticEnabled = enabled;
  93. }
  94. Future<void> dispose() async {
  95. await _backgroundPlayer.dispose();
  96. await _effectPlayer.dispose();
  97. }
  98. }