Timer ist running most of the time with 3 different sounds

main
Parricc35 2023-02-19 21:55:29 +01:00
parent fb31d1db2c
commit f38b2fd903
9 changed files with 100 additions and 71 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
assets/finish.mp3 100644

Binary file not shown.

BIN
assets/go.mp3 100644

Binary file not shown.

BIN
assets/warmUp.mp3 100644

Binary file not shown.

BIN
assets/workout.mp3 100644

Binary file not shown.

View File

@ -12,11 +12,15 @@ class IntervalTimerPage extends StatefulWidget {
class _IntervalTimerPageState extends State<IntervalTimerPage> {
final Duration _warmupDuration = const Duration(seconds: 5);
final Duration _cooldownDuration = const Duration(seconds: 5);
final Duration _highIntensityDuration = const Duration(seconds: 4);
final Duration _lowIntensityDuration = const Duration(seconds: 3);
late Duration _totalDuration = const Duration(seconds: 35);
final Duration _highIntensityDuration = const Duration(seconds: 3);
final Duration _lowIntensityDuration = const Duration(seconds: 2);
late Duration _totalDuration = const Duration(minutes: 35);
final highIntensityPlayer = AudioPlayer();
final lowIntensityPlayer = AudioPlayer();
final coolDownPlayer = AudioPlayer();
final int _numHighIntensityBlocks = 4;
final int _numLowIntensityBlocks = 3;
Timer? _timer;
int _currentBlock = 0;
Duration _currentDuration = const Duration();
@ -36,15 +40,19 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
void _startTimer() {
_isPaused = false;
() async {
await AudioPlayer().play(UrlSource('assets/go.mp3'));
}();
_timer = Timer.periodic(const Duration(seconds: 1), (_) => _tick());
}
void _pauseTimer() {
_isPaused = true;
_timer?.cancel();
Future.delayed(const Duration(seconds: 1)).then((value) {
_playHighIntensityMusic();
});
}
void _resetTimer() {
coolDownPlayer.stop();
highIntensityPlayer.stop();
lowIntensityPlayer.stop();
_isPaused = true;
_timer?.cancel();
_currentBlock = 0;
@ -53,9 +61,20 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
setState(() {});
}
Future<void> _playSoundEffect() async {
final player = AudioPlayer();
await player.play(UrlSource('assets/beep.mp3'));
Future<void> _playHighIntensityMusic() async {
await highIntensityPlayer.setReleaseMode(ReleaseMode.loop);
await highIntensityPlayer.play(UrlSource('assets/warmUp.mp3'));
}
Future<void> _playLowIntensityMusic() async {
await highIntensityPlayer.stop();
Future.delayed(const Duration(microseconds: 600)).then((value) async {
await lowIntensityPlayer.play(UrlSource('assets/workout.mp3'));
});
}
Future<void> _intervalChange() async {
await AudioPlayer().play(UrlSource('assets/beep.mp3'));
}
void _tick() {
@ -66,27 +85,35 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
_totalDuration = Duration(
seconds: _totalDuration.inSeconds - 1,
);
if (_currentDuration.inSeconds == 0) {
_playSoundEffect();
}
if (_currentDuration.inSeconds < 0) {
if (_currentDuration.inSeconds < 1) {
if (_currentBlock < _numHighIntensityBlocks + _numLowIntensityBlocks) {
_intervalChange();
if (_currentBlock == 0) {
// Start high intensity blocks.
_currentBlock++;
_currentDuration = _highIntensityDuration;
} else if (_currentBlock <= _numHighIntensityBlocks) {
// Start low intensity blocks.
_currentBlock++;
_currentDuration = _lowIntensityDuration;
} else if (_currentBlock <=
_numHighIntensityBlocks + _numLowIntensityBlocks) {
// Start high intensity blocks again.
_playLowIntensityMusic();
}
_currentBlock++;
if (_currentBlock % 2 == 1) {
_currentDuration = _highIntensityDuration;
} else {
// End workout.
_currentDuration = _lowIntensityDuration;
}
} else if (_currentBlock < _numHighIntensityBlocks * 2) {
_intervalChange();
_currentBlock++;
_currentDuration = _cooldownDuration;
_pauseTimer();
() async {
await lowIntensityPlayer.stop();
await coolDownPlayer.play(UrlSource('assets/cool_down.mp3'));
}();
} else {
() async {
Future.delayed(const Duration(microseconds: 900))
.then((value) async {
await AudioPlayer().play(UrlSource('assets/finish.mp3'));
});
}();
_resetTimer();
}
}
});
@ -108,9 +135,6 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Interval Timer'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
@ -118,9 +142,11 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
Text(
_currentBlock == 0
? 'Warm-up'
: _currentBlock <= _numHighIntensityBlocks
: _currentBlock % 2 == 1
? 'High Intensity'
: 'Low Intensity',
: _currentBlock < _numHighIntensityBlocks * 2
? 'Low Intensity'
: 'Cool-down',
style: const TextStyle(fontSize: 32.0),
),
const SizedBox(height: 16.0),
@ -138,15 +164,13 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(_isPaused
? Icons.play_arrow_rounded
: Icons.stop_rounded),
icon: Icon(
_isPaused ? Icons.play_arrow_rounded : Icons.stop_rounded),
iconSize: 48.0,
onPressed: () {
if (_isPaused) {
_startTimer();
} else {
_pauseTimer();
_resetTimer();
}
},

View File

@ -63,6 +63,11 @@ flutter:
# To add assets to your application, add an assets section, like this:
assets:
- beep.mp3
- go.mp3
- workout.mp3
- warmUp.mp3
- cool_down.mp3
- finish.mp3
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg