fix onlisten stream error
parent
5587f049e1
commit
fa8d2f6314
|
@ -1,3 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:audioplayers/audioplayers.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:smoke_cess_app/models/workout.dart';
|
||||
|
@ -8,9 +10,11 @@ class WorkoutProvider extends ChangeNotifier {
|
|||
TimerProvider timerProvider;
|
||||
|
||||
final AudioPlayer _audioPlayer = AudioPlayer();
|
||||
late StreamSubscription _onCompleteSubscription;
|
||||
final Source _finishedSoundSource = AssetSource('finish.mp3');
|
||||
final Source _beepSoundSource = AssetSource('beep.mp3');
|
||||
bool isWorkoutStarted = false;
|
||||
bool isWorkoutComplete = false;
|
||||
bool isMuted = false;
|
||||
int motivationBefore = 50;
|
||||
int motivationAfter = 50;
|
||||
|
@ -50,18 +54,19 @@ class WorkoutProvider extends ChangeNotifier {
|
|||
Color get currentPhaseColor => _workoutPhaseSettings[currentPhase]!['color'];
|
||||
AssetSource get currentPhaseSource =>
|
||||
_workoutPhaseSettings[currentPhase]!['source'];
|
||||
bool get isWorkoutComplete =>
|
||||
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete;
|
||||
/* bool get isWorkoutComplete =>
|
||||
_workoutPhaseIndex == _workoutPhases.length - 1 && isPhaseComplete; */
|
||||
|
||||
void nextPhase() {
|
||||
_onCompleteSubscription.cancel();
|
||||
_audioPlayer.stop();
|
||||
if (_workoutPhaseIndex < _workoutPhases.length - 1) {
|
||||
_audioPlayer.play(_beepSoundSource);
|
||||
_workoutPhaseIndex += 1;
|
||||
_audioPlayer.onPlayerComplete.listen((event) {
|
||||
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
||||
_audioPlayer.play(currentPhaseSource);
|
||||
timerProvider.startTimer(currentPhaseDuration);
|
||||
});
|
||||
timerProvider.startTimer(currentPhaseDuration);
|
||||
} else {
|
||||
//workout completed
|
||||
_audioPlayer.play(_finishedSoundSource);
|
||||
|
@ -71,19 +76,20 @@ class WorkoutProvider extends ChangeNotifier {
|
|||
|
||||
void startWorkout() {
|
||||
isWorkoutStarted = true;
|
||||
_audioPlayer.play(_beepSoundSource);
|
||||
_audioPlayer.onPlayerComplete.listen((event) {
|
||||
isWorkoutComplete = false;
|
||||
_audioPlayer.play(_beepSoundSource).whenComplete(() => null);
|
||||
_onCompleteSubscription = _audioPlayer.onPlayerComplete.listen((event) {
|
||||
_audioPlayer.play(currentPhaseSource);
|
||||
timerProvider.startTimer(currentPhaseDuration);
|
||||
});
|
||||
timerProvider.startTimer(currentPhaseDuration);
|
||||
}
|
||||
|
||||
void stopWorkout() {
|
||||
isWorkoutStarted = false;
|
||||
//_workoutPhaseIndex = 0;
|
||||
isWorkoutComplete = true;
|
||||
_audioPlayer.stop();
|
||||
timerProvider.stopTimer();
|
||||
notifyListeners();
|
||||
//notifyListeners();
|
||||
}
|
||||
|
||||
void saveWorkout() {
|
||||
|
|
|
@ -16,7 +16,7 @@ class WorkoutTimerWidget extends StatelessWidget {
|
|||
TimerProvider timerProvider = context.watch<TimerProvider>();
|
||||
WorkoutProvider workoutProvider = context.watch<WorkoutProvider>();
|
||||
|
||||
if (workoutProvider.isPhaseComplete && workoutProvider.isWorkoutStarted) {
|
||||
if (workoutProvider.isPhaseComplete && !workoutProvider.isWorkoutComplete) {
|
||||
Timer(const Duration(milliseconds: 1), () => workoutProvider.nextPhase());
|
||||
}
|
||||
|
||||
|
@ -36,10 +36,6 @@ class WorkoutTimerWidget extends StatelessWidget {
|
|||
}
|
||||
}
|
||||
|
||||
if (workoutProvider.isWorkoutComplete) {
|
||||
handleStartStopWorkout();
|
||||
}
|
||||
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
|
|
Loading…
Reference in New Issue