1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import 'package:audioplayers/audioplayers.dart';
- import 'package:vibration/vibration.dart';
- class AudioManager {
- static final AudioManager _instance = AudioManager._internal();
- final AudioPlayer _audioPlayer = AudioPlayer();
- bool _isPlaying = false;
-
- factory AudioManager() {
- return _instance;
- }
-
- AudioManager._internal();
-
- Future<void> playSound(String sound, double volume, bool loop) async {
- if (_isPlaying) {
- await stopSound();
- }
-
- // For this example, we're using dummy sound mapping
- // In a real app, you would have actual sound files
- String soundAsset;
- switch (sound) {
- case 'Dripping':
- soundAsset = 'assets/sounds/dripping.mp3';
- break;
- case 'Alarm':
- soundAsset = 'assets/sounds/alarm.mp3';
- break;
- case 'Bell':
- soundAsset = 'assets/sounds/bell.mp3';
- break;
- default:
- soundAsset = 'assets/sounds/dripping.mp3';
- }
-
- await _audioPlayer.setVolume(volume);
- await _audioPlayer.setReleaseMode(loop ? ReleaseMode.loop : ReleaseMode.release);
- await _audioPlayer.play(AssetSource(soundAsset));
- _isPlaying = true;
- }
-
- Future<void> stopSound() async {
- await _audioPlayer.stop();
- _isPlaying = false;
- }
-
- Future<void> triggerVibration() async {
- if (await Vibration.hasVibrator() ?? false) {
- Vibration.vibrate(pattern: [500, 1000, 500, 1000], repeat: 1);
- }
- }
-
- Future<void> stopVibration() async {
- if (await Vibration.hasVibrator() ?? false) {
- Vibration.cancel();
- }
- }
-
- Future<void> dispose() async {
- await stopSound();
- await stopVibration();
- await _audioPlayer.dispose();
- }
- }
|