cpd_2022_zi/lib/providers/workout_provider.dart

90 lines
2.5 KiB
Dart

import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart';
class WorkoutProvider extends ChangeNotifier {
TimerProvider timerProvider;
final AudioPlayer _audioPlayer = AudioPlayer();
final Source _finishedSoundSource = AssetSource('finish.mp3');
final Source _beepSoundSource = AssetSource('beep.mp3');
bool isWorkoutStarted = false;
bool isMuted = false;
void mutePlayer() {
isMuted = true;
_audioPlayer.setVolume(0);
notifyListeners();
}
void unMutePlayer() {
isMuted = false;
_audioPlayer.setVolume(1);
notifyListeners();
}
WorkoutProvider(this.timerProvider);
final List<String> _workoutPhases = [
'Warm-Up',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Low Intensity',
'High Intensity',
'Cool-down'
];
final Map<String, Duration> _phasesDuration = {
'Warm-Up': const Duration(seconds: 5),
'High Intensity': const Duration(seconds: 4),
'Low Intensity': const Duration(seconds: 3),
'Cool-down': const Duration(seconds: 5)
};
final Map<String, Source> _phaseSongSources = {
'Warm-Up': AssetSource('warmUp.mp3'),
'High Intensity': AssetSource('workout.mp3'),
'Low Intensity': AssetSource('workout.mp3'),
'Cool-down': AssetSource('cool_down.mp3')
};
int _workoutPhaseIndex = 0;
String get currentPhase => _workoutPhases[_workoutPhaseIndex];
Duration get currentPhaseDuration =>
_phasesDuration[currentPhase] ?? const Duration(seconds: 0);
bool get isPhaseComplete =>
timerProvider.elapsedSeconds - currentPhaseDuration.inSeconds == 0;
void nextPhase() {
_audioPlayer.stop();
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
_audioPlayer.play(_beepSoundSource);
_workoutPhaseIndex += 1;
_audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
});
} else {
_audioPlayer.play(_finishedSoundSource);
}
}
void startWorkout() {
isWorkoutStarted = true;
_audioPlayer.play(_beepSoundSource);
_audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
});
}
void stopWorkout() {
isWorkoutStarted = false;
_workoutPhaseIndex = 0;
_audioPlayer.stop();
timerProvider.stopTimer();
notifyListeners();
}
}