diff --git a/assets/autobahn-99109.mp3 b/assets/autobahn-99109.mp3 new file mode 100644 index 0000000..21d71c8 Binary files /dev/null and b/assets/autobahn-99109.mp3 differ diff --git a/assets/beep.mp3 b/assets/beep.mp3 index cd3a3ec..fdcaa16 100644 Binary files a/assets/beep.mp3 and b/assets/beep.mp3 differ diff --git a/assets/cool_down.mp3 b/assets/cool_down.mp3 new file mode 100644 index 0000000..fc6504b Binary files /dev/null and b/assets/cool_down.mp3 differ diff --git a/assets/finish.mp3 b/assets/finish.mp3 new file mode 100644 index 0000000..de51d68 Binary files /dev/null and b/assets/finish.mp3 differ diff --git a/assets/go.mp3 b/assets/go.mp3 new file mode 100644 index 0000000..ecaaa16 Binary files /dev/null and b/assets/go.mp3 differ diff --git a/assets/warmUp.mp3 b/assets/warmUp.mp3 new file mode 100644 index 0000000..68aa61f Binary files /dev/null and b/assets/warmUp.mp3 differ diff --git a/assets/workout.mp3 b/assets/workout.mp3 new file mode 100644 index 0000000..db0cabd Binary files /dev/null and b/assets/workout.mp3 differ diff --git a/lib/pages/timer_page.dart b/lib/pages/timer_page.dart index 06cbabc..b46acf6 100644 --- a/lib/pages/timer_page.dart +++ b/lib/pages/timer_page.dart @@ -12,11 +12,15 @@ class IntervalTimerPage extends StatefulWidget { class _IntervalTimerPageState extends State { 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 { 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 { setState(() {}); } - Future _playSoundEffect() async { - final player = AudioPlayer(); - await player.play(UrlSource('assets/beep.mp3')); + Future _playHighIntensityMusic() async { + await highIntensityPlayer.setReleaseMode(ReleaseMode.loop); + await highIntensityPlayer.play(UrlSource('assets/warmUp.mp3')); + } + + Future _playLowIntensityMusic() async { + await highIntensityPlayer.stop(); + Future.delayed(const Duration(microseconds: 600)).then((value) async { + await lowIntensityPlayer.play(UrlSource('assets/workout.mp3')); + }); + } + + Future _intervalChange() async { + await AudioPlayer().play(UrlSource('assets/beep.mp3')); } void _tick() { @@ -66,27 +85,35 @@ class _IntervalTimerPageState extends State { _totalDuration = Duration( seconds: _totalDuration.inSeconds - 1, ); - if (_currentDuration.inSeconds == 0) { - _playSoundEffect(); - } - if (_currentDuration.inSeconds < 0) { - if (_currentBlock == 0) { - // Start high intensity blocks. + if (_currentDuration.inSeconds < 1) { + if (_currentBlock < _numHighIntensityBlocks + _numLowIntensityBlocks) { + _intervalChange(); + if (_currentBlock == 0) { + _playLowIntensityMusic(); + } _currentBlock++; - _currentDuration = _highIntensityDuration; - } else if (_currentBlock <= _numHighIntensityBlocks) { - // Start low intensity blocks. + + if (_currentBlock % 2 == 1) { + _currentDuration = _highIntensityDuration; + } else { + _currentDuration = _lowIntensityDuration; + } + } else if (_currentBlock < _numHighIntensityBlocks * 2) { + _intervalChange(); _currentBlock++; - _currentDuration = _lowIntensityDuration; - } else if (_currentBlock <= - _numHighIntensityBlocks + _numLowIntensityBlocks) { - // Start high intensity blocks again. - _currentBlock++; - _currentDuration = _highIntensityDuration; - } else { - // End workout. _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,53 +135,50 @@ class _IntervalTimerPageState extends State { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: const Text('Interval Timer'), - ), body: Center( child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + _currentBlock == 0 + ? 'Warm-up' + : _currentBlock % 2 == 1 + ? 'High Intensity' + : _currentBlock < _numHighIntensityBlocks * 2 + ? 'Low Intensity' + : 'Cool-down', + style: const TextStyle(fontSize: 32.0), + ), + const SizedBox(height: 16.0), + Text( + _formatDuration(_currentDuration), + style: const TextStyle(fontSize: 80.0), + ), + const SizedBox(height: 32.0), + Text( + 'Total: ${_formatTotalDuration(_totalDuration)}', + style: const TextStyle(fontSize: 24.0), + ), + const SizedBox(height: 32.0), + Row( mainAxisAlignment: MainAxisAlignment.center, children: [ - Text( - _currentBlock == 0 - ? 'Warm-up' - : _currentBlock <= _numHighIntensityBlocks - ? 'High Intensity' - : 'Low Intensity', - style: const TextStyle(fontSize: 32.0), - ), - const SizedBox(height: 16.0), - Text( - _formatDuration(_currentDuration), - style: const TextStyle(fontSize: 80.0), - ), - const SizedBox(height: 32.0), - Text( - 'Total: ${_formatTotalDuration(_totalDuration)}', - style: const TextStyle(fontSize: 24.0), - ), - const SizedBox(height: 32.0), - Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - IconButton( - icon: Icon(_isPaused - ? Icons.play_arrow_rounded - : Icons.stop_rounded), - iconSize: 48.0, - onPressed: () { - if (_isPaused) { - _startTimer(); - } else { - _pauseTimer(); - _resetTimer(); - } - }, - ), - // ), - ], + IconButton( + icon: Icon( + _isPaused ? Icons.play_arrow_rounded : Icons.stop_rounded), + iconSize: 48.0, + onPressed: () { + if (_isPaused) { + _startTimer(); + } else { + _resetTimer(); + } + }, ), + // ), ], - ))); + ), + ], + ))); } } diff --git a/pubspec.yaml b/pubspec.yaml index acbffcf..5eb18b2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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