audio_manager.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import 'package:flutter/services.dart';
  2. import 'package:flame_audio/flame_audio.dart';
  3. import 'dart:math';
  4. import 'dart:io' show Platform;
  5. class AudioManager {
  6. static final AudioManager _instance = AudioManager._internal();
  7. factory AudioManager() => _instance;
  8. AudioManager._internal();
  9. final Random _random = Random();
  10. bool _soundEnabled = true;
  11. bool _musicEnabled = true;
  12. bool _hapticEnabled = true;
  13. double _bgmVolume = 0.4; // 40% default
  14. double _sfxVolume = 0.6; // 60% default
  15. bool _isAudioSupported = false;
  16. bool _isVibrationSupported = false;
  17. AudioPool? _popPool;
  18. AudioPool? _popAltPool;
  19. bool get soundEnabled => _soundEnabled;
  20. bool get musicEnabled => _musicEnabled;
  21. bool get hapticEnabled => _hapticEnabled;
  22. double get bgmVolume => _bgmVolume;
  23. double get sfxVolume => _sfxVolume;
  24. bool get isAudioSupported => _isAudioSupported;
  25. bool get isVibrationSupported => _isVibrationSupported;
  26. Future<void> initialize() async {
  27. await _detectPlatformCapabilities();
  28. if (_isAudioSupported) {
  29. // Preload audio assets
  30. try {
  31. await FlameAudio.audioCache.loadAll([
  32. 'source/ambient_background.mp3',
  33. 'source/bubble_pop.mp3',
  34. 'source/bubble_pop_alt.mp3'
  35. ]);
  36. // Create sound pools for efficient playback
  37. _popPool = await FlameAudio.createPool(
  38. 'source/bubble_pop.mp3',
  39. maxPlayers: 5,
  40. );
  41. _popAltPool = await FlameAudio.createPool(
  42. 'source/bubble_pop_alt.mp3',
  43. maxPlayers: 5,
  44. );
  45. } catch (e) {
  46. print('Failed to load audio assets or create pools: $e');
  47. }
  48. }
  49. print('AudioManager initialized for ${Platform.operatingSystem}');
  50. }
  51. Future<void> _detectPlatformCapabilities() async {
  52. // Check audio support
  53. _isAudioSupported = _checkAudioSupport();
  54. // Check vibration support
  55. _isVibrationSupported = _checkVibrationSupport();
  56. print('Platform capabilities - Audio: $_isAudioSupported, Vibration: $_isVibrationSupported');
  57. }
  58. bool _checkAudioSupport() {
  59. try {
  60. // Just Audio supports Android, iOS, web, macOS, Windows and Linux
  61. // See: https://pub.dev/packages/just_audio
  62. return true;
  63. } catch (e) {
  64. print('Audio detection failed: $e');
  65. return false;
  66. }
  67. }
  68. bool _checkVibrationSupport() {
  69. try {
  70. // Only Android and iOS support vibration via HapticFeedback
  71. if (Platform.isAndroid || Platform.isIOS) {
  72. return true;
  73. }
  74. return false;
  75. } catch (e) {
  76. print('Vibration detection failed: $e');
  77. return false;
  78. }
  79. }
  80. void playBubblePop() {
  81. if (!_soundEnabled) return;
  82. // Make audio playback async to avoid blocking main thread
  83. _playBubblePopAsync();
  84. // Handle haptic feedback asynchronously
  85. if (_hapticEnabled && _isVibrationSupported) {
  86. _playHapticFeedbackAsync();
  87. }
  88. }
  89. void _playBubblePopAsync() async {
  90. if (_isAudioSupported) {
  91. // Use Flame Audio for bubble pop
  92. final sound = _random.nextBool()
  93. ? 'source/bubble_pop.mp3'
  94. : 'source/bubble_pop_alt.mp3';
  95. try {
  96. if (sound == 'source/bubble_pop.mp3' && _popPool != null) {
  97. _popPool!.start(volume: _sfxVolume);
  98. } else if (sound == 'source/bubble_pop_alt.mp3' && _popAltPool != null) {
  99. _popAltPool!.start(volume: _sfxVolume);
  100. } else {
  101. // Fallback to direct playback if pools fail
  102. FlameAudio.play(sound, volume: _sfxVolume);
  103. }
  104. } catch (e) {
  105. print('Failed to play bubble pop sound: $e');
  106. }
  107. } else {
  108. // Fallback to system sound
  109. _playSystemSoundAsync();
  110. }
  111. }
  112. void _playSystemSoundAsync() async {
  113. try {
  114. // Vary the system sound for some variety
  115. final soundType = _random.nextInt(3);
  116. switch (soundType) {
  117. case 0:
  118. await SystemSound.play(SystemSoundType.click);
  119. break;
  120. case 1:
  121. await SystemSound.play(SystemSoundType.alert);
  122. break;
  123. default:
  124. await SystemSound.play(SystemSoundType.click);
  125. }
  126. } catch (e) {
  127. print('Failed to play system sound: $e');
  128. }
  129. }
  130. void _playHapticFeedbackAsync() async {
  131. if (!_isVibrationSupported) {
  132. // Silently skip haptic feedback on unsupported platforms
  133. return;
  134. }
  135. try {
  136. // Use Flutter's built-in HapticFeedback instead of vibration plugin
  137. await HapticFeedback.lightImpact();
  138. } catch (e) {
  139. print('Failed to play haptic feedback: $e');
  140. // Disable vibration support if it fails
  141. _isVibrationSupported = false;
  142. }
  143. }
  144. Future<void> playBackgroundMusic() async {
  145. if (!_musicEnabled || !_isAudioSupported) {
  146. if (!_isAudioSupported) {
  147. print('Background music not available - using system audio only');
  148. }
  149. return;
  150. }
  151. try {
  152. // Stop any existing BGM first
  153. await FlameAudio.bgm.stop();
  154. // Play with updated volume
  155. FlameAudio.bgm.play(
  156. 'source/ambient_background.mp3',
  157. volume: _bgmVolume
  158. );
  159. print('Background music started');
  160. } catch (e) {
  161. print('Failed to play background music: $e');
  162. }
  163. }
  164. Future<void> stopBackgroundMusic() async {
  165. if (!_isAudioSupported) return;
  166. try {
  167. FlameAudio.bgm.stop();
  168. } catch (e) {
  169. print('Failed to stop background music: $e');
  170. }
  171. }
  172. Future<void> pauseBackgroundMusic() async {
  173. if (!_isAudioSupported) return;
  174. try {
  175. FlameAudio.bgm.pause();
  176. } catch (e) {
  177. print('Failed to pause background music: $e');
  178. }
  179. }
  180. Future<void> resumeBackgroundMusic() async {
  181. if (!_musicEnabled || !_isAudioSupported) return;
  182. try {
  183. FlameAudio.bgm.resume();
  184. } catch (e) {
  185. print('Failed to resume background music: $e');
  186. }
  187. }
  188. void setSoundEnabled(bool enabled) {
  189. _soundEnabled = enabled;
  190. }
  191. void setMusicEnabled(bool enabled) {
  192. _musicEnabled = enabled;
  193. if (!enabled) {
  194. stopBackgroundMusic();
  195. } else {
  196. playBackgroundMusic();
  197. }
  198. }
  199. void setBgmVolume(double volume) {
  200. _bgmVolume = volume;
  201. // Volume will be applied when music is next played
  202. }
  203. void setSfxVolume(double volume) {
  204. _sfxVolume = volume;
  205. // No effect with system audio
  206. }
  207. void setHapticEnabled(bool enabled) {
  208. _hapticEnabled = enabled;
  209. }
  210. Future<void> dispose() async {
  211. try {
  212. await FlameAudio.bgm.dispose();
  213. } catch (e) {
  214. print('Failed to dispose background player: $e');
  215. }
  216. try {
  217. await _popPool?.dispose();
  218. } catch (e) {
  219. print('Failed to dispose popPool: $e');
  220. }
  221. try {
  222. await _popAltPool?.dispose();
  223. } catch (e) {
  224. print('Failed to dispose popAltPool: $e');
  225. }
  226. }
  227. // Utility method to get platform info for debugging
  228. String getPlatformInfo() {
  229. final audioType = _isAudioSupported ? 'Full Audio Support' : 'System Sounds Only';
  230. return 'Platform: ${Platform.operatingSystem}, '
  231. 'Audio: $audioType, '
  232. 'Vibration: $_isVibrationSupported';
  233. }
  234. }