Skip to content

Commit

Permalink
feat: stop timer on stop
Browse files Browse the repository at this point in the history
  • Loading branch information
iosifnicolae2 committed Aug 5, 2023
1 parent f126332 commit e125666
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/appAudioHandler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
List<Query$GetStations$stations> stations =
List<Query$GetStations$stations>.empty(growable: true);
List<MediaItem> stationsMediaItems = List<MediaItem>.empty(growable: true);
var _timer;

Object? error;
int errorRetryCount = 0;
Expand Down Expand Up @@ -88,8 +89,6 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
});

await _player.setLoopMode(LoopMode.off);

startListeningTracker();
}

@override
Expand Down Expand Up @@ -141,7 +140,7 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
AppTracking.trackPlayStation(currentStation!);
AppTracking.trackListenStation(currentStation!, currentStreamUrl);
}
// _player.stop();
startListeningTracker();
return _player.play();
}

Expand All @@ -155,6 +154,7 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
Future<void> seek(final Duration? position, {int? index}) {
developer.log("seek: $position $index");
_playerIndex = index ?? 0;
stopListeningTracker();
return setAudioSource(index ?? 0);
}

Expand Down Expand Up @@ -304,7 +304,7 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
}

void startListeningTracker() {
Timer.periodic(
_timer = Timer.periodic(
const Duration(seconds: 5),
(Timer t) => {
if (currentStation != null && _player.playing)
Expand All @@ -315,6 +315,10 @@ class AppAudioHandler extends BaseAudioHandler with QueueHandler {
});
}

void stopListeningTracker() {
_timer?.cancel();
}

String get currentStreamUrl {
return mediaItem.value?.id ?? "-";
}
Expand Down

0 comments on commit e125666

Please sign in to comment.