fix onlisten stream error

main
Julian Gegner 2023-03-02 15:08:39 +01:00
parent 5587f049e1
commit fa8d2f6314
2 changed files with 16 additions and 14 deletions

View File

@ -1,3 +1,5 @@
import 'dart:async';
import 'package:audioplayers/audioplayers.dart'; import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:smoke_cess_app/models/workout.dart'; import 'package:smoke_cess_app/models/workout.dart';
@ -8,9 +10,11 @@ class WorkoutProvider extends ChangeNotifier {
TimerProvider timerProvider; TimerProvider timerProvider;
final AudioPlayer _audioPlayer = AudioPlayer(); final AudioPlayer _audioPlayer = AudioPlayer();
late StreamSubscription _onCompleteSubscription;
final Source _finishedSoundSource = AssetSource('finish.mp3'); final Source _finishedSoundSource = AssetSource('finish.mp3');
final Source _beepSoundSource = AssetSource('beep.mp3'); final Source _beepSoundSource = AssetSource('beep.mp3');
bool isWorkoutStarted = false; bool isWorkoutStarted = false;
bool isWorkoutComplete = false;
bool isMuted = false; bool isMuted = false;
int motivationBefore = 50; int motivationBefore = 50;
int motivationAfter = 50; int motivationAfter = 50;
@ -50,18 +54,19 @@ class WorkoutProvider extends ChangeNotifier {
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color']; Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
AssetSource get currentPhaseSource => AssetSource get currentPhaseSource =>
_workoutPhaseSettings[currentPhase]!['source']; _workoutPhaseSettings[currentPhase]!['source'];
bool get isWorkoutComplete => /* bool get isWorkoutComplete =>
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; _workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */
void nextPhase() { void nextPhase() {
_onCompleteSubscription.cancel();
_audioPlayer.stop(); _audioPlayer.stop();
if (_workoutPhaseIndex < _workoutPhases.length - 1) { if (_workoutPhaseIndex < _workoutPhases.length - 1) {
_audioPlayer.play(_beepSoundSource); _audioPlayer.play(_beepSoundSource);
_workoutPhaseIndex += 1; _workoutPhaseIndex += 1;
_audioPlayer.onPlayerComplete.listen((event) { _onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(currentPhaseSource); _audioPlayer.play(currentPhaseSource);
timerProvider.startTimer(currentPhaseDuration);
}); });
timerProvider.startTimer(currentPhaseDuration);
} else { } else {
//workout completed //workout completed
_audioPlayer.play(_finishedSoundSource); _audioPlayer.play(_finishedSoundSource);
@ -71,19 +76,20 @@ class WorkoutProvider extends ChangeNotifier {
void startWorkout() { void startWorkout() {
isWorkoutStarted = true; isWorkoutStarted = true;
_audioPlayer.play(_beepSoundSource); isWorkoutComplete = false;
_audioPlayer.onPlayerComplete.listen((event) { _audioPlayer.play(_beepSoundSource).whenComplete(() => null);
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
_audioPlayer.play(currentPhaseSource); _audioPlayer.play(currentPhaseSource);
timerProvider.startTimer(currentPhaseDuration);
}); });
timerProvider.startTimer(currentPhaseDuration);
} }
void stopWorkout() { void stopWorkout() {
isWorkoutStarted = false; isWorkoutStarted = false;
//_workoutPhaseIndex = 0; isWorkoutComplete = true;
_audioPlayer.stop(); _audioPlayer.stop();
timerProvider.stopTimer(); timerProvider.stopTimer();
notifyListeners(); //notifyListeners();
} }
void saveWorkout() { void saveWorkout() {

View File

@ -16,7 +16,7 @@ class WorkoutTimerWidget extends StatelessWidget {
TimerProvider timerProvider = context.watch<TimerProvider>(); TimerProvider timerProvider = context.watch<TimerProvider>();
WorkoutProvider workoutProvider = context.watch<WorkoutProvider>(); WorkoutProvider workoutProvider = context.watch<WorkoutProvider>();
if (workoutProvider.isPhaseComplete && workoutProvider.isWorkoutStarted) { if (workoutProvider.isPhaseComplete && !workoutProvider.isWorkoutComplete) {
Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase()); Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase());
} }
@ -36,10 +36,6 @@ class WorkoutTimerWidget extends StatelessWidget {
} }
} }
if (workoutProvider.isWorkoutComplete) {
handleStartStopWorkout();
}
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [