123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 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();
- }
- 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) {
- // Vibrate continuously for 5 seconds
- Vibration.vibrate(duration: 1000);
- }
- }
- Future<void> stopVibration() async {
- if (await Vibration.hasVibrator() ?? false) {
- Vibration.cancel();
- }
- }
- Future<void> dispose() async {
- await stopSound();
- await stopVibration();
- await _audioPlayer.dispose();
- }
- }
|