playing music and iterating throug workout

main
Julian Gegner 2023-02-28 13:05:51 +01:00
parent ec5f23845d
commit b01485f27a
4 changed files with 29 additions and 10 deletions

View File

@ -170,10 +170,12 @@ class _IntervalTimerPageState extends State<IntervalTimerPage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
TimerProvider timerProvider = TimerProvider();
return MultiProvider( return MultiProvider(
providers: [ providers: [
ChangeNotifierProvider(create: (context) => TimerProvider()), ChangeNotifierProvider(create: (context) => timerProvider),
ChangeNotifierProvider(create: (context) => WorkoutProvider()) ChangeNotifierProvider(
create: (context) => WorkoutProvider(timerProvider))
], ],
child: WorkoutTimerWidget(), child: WorkoutTimerWidget(),
); );

View File

@ -11,7 +11,6 @@ class TimerProvider extends ChangeNotifier {
started = true; started = true;
_timer = Timer.periodic(const Duration(seconds: 1), ((timer) { _timer = Timer.periodic(const Duration(seconds: 1), ((timer) {
if (timer.tick >= duration.inSeconds) { if (timer.tick >= duration.inSeconds) {
print(elapsedSeconds);
timer.cancel(); timer.cancel();
started = false; started = false;
} }

View File

@ -1,6 +1,16 @@
import 'dart:async';
import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/providers/timer_provider.dart';
class WorkoutProvider extends ChangeNotifier { class WorkoutProvider extends ChangeNotifier {
TimerProvider timerProvider;
final AudioPlayer _audioPlayer = AudioPlayer();
WorkoutProvider(this.timerProvider);
final List<String> _workoutPhases = [ final List<String> _workoutPhases = [
'Warm-Up', 'Warm-Up',
'High Intensity', 'High Intensity',
@ -18,6 +28,12 @@ class WorkoutProvider extends ChangeNotifier {
'Low Intensity': const Duration(seconds: 3), 'Low Intensity': const Duration(seconds: 3),
'Cool-down': const Duration(seconds: 5) '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; int _workoutPhaseIndex = 0;
String get currentPhase => _workoutPhases[_workoutPhaseIndex]; String get currentPhase => _workoutPhases[_workoutPhaseIndex];
@ -25,9 +41,11 @@ class WorkoutProvider extends ChangeNotifier {
_phasesDuration[currentPhase] ?? const Duration(seconds: 0); _phasesDuration[currentPhase] ?? const Duration(seconds: 0);
void nextPhase() { void nextPhase() {
_workoutPhaseIndex < _workoutPhases.length _audioPlayer.stop();
? _workoutPhaseIndex += 1 if (_workoutPhaseIndex < _workoutPhases.length - 1) {
: _workoutPhaseIndex = 0; _workoutPhaseIndex += 1;
//notifyListeners(); _audioPlayer.play(_phaseSongSources[currentPhase]!);
timerProvider.startTimer(currentPhaseDuration);
}
} }
} }

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:smoke_cess_app/providers/workout_provider.dart'; import 'package:smoke_cess_app/providers/workout_provider.dart';
@ -15,9 +17,7 @@ class WorkoutTimerWidget extends StatelessWidget {
if (timerProvider.elapsedSeconds == if (timerProvider.elapsedSeconds ==
workoutProvider.currentPhaseDuration.inSeconds) { workoutProvider.currentPhaseDuration.inSeconds) {
print('Timer abgelaufen'); Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase());
workoutProvider.nextPhase();
timerProvider.startTimer(workoutProvider.currentPhaseDuration);
} }
return Column( return Column(