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

View File

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