12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- 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 = 'sounds/dripping.mp3';
- break;
- case 'Alarm':
- soundAsset = 'sounds/alarm.mp3';
- break;
- case 'Bell':
- soundAsset = 'sounds/bell.mp3';
- break;
- default:
- soundAsset = '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);
- Vibration.vibrate(
- pattern: [0, 500, 200, 500, 200, 500],
- intensities: [0, 255, 0, 255, 0, 255],
- );
- }
- }
- Future<void> stopVibration() async {
- if (await Vibration.hasVibrator() ?? false) {
- Vibration.cancel();
- }
- }
- Future<void> dispose() async {
- await stopSound();
- await stopVibration();
- await _audioPlayer.dispose();
- }
- }
|