mirror of
https://github.com/Dr-Blank/Vaani.git
synced 2026-02-16 14:29:35 +00:00
58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:vaani/features/player/core/audiobook_player_session.dart';
|
|
import 'package:vaani/shared/extensions/chapter.dart';
|
|
import 'package:vaani/shared/utils/throttler.dart';
|
|
|
|
class SkipStartEnd {
|
|
final Duration start;
|
|
final Duration end;
|
|
final AbsAudioHandler player;
|
|
|
|
final List<StreamSubscription> _subscriptions = [];
|
|
final throttlerStart = Throttler(delay: Duration(seconds: 3));
|
|
final throttlerEnd = Throttler(delay: Duration(seconds: 3));
|
|
|
|
SkipStartEnd({
|
|
required this.start,
|
|
required this.end,
|
|
required this.player,
|
|
}) {
|
|
if (start > Duration.zero) {
|
|
_subscriptions.add(
|
|
player.chapterStream.listen((chapter) {
|
|
if (chapter != null &&
|
|
player.positionInChapter < Duration(seconds: 1)) {
|
|
Future.microtask(
|
|
() => throttlerStart
|
|
.call(() => player.seekInBook(chapter.start + start)),
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
if (end > Duration.zero) {
|
|
_subscriptions.add(
|
|
player.positionStreamInChapter.listen((positionChapter) {
|
|
if (end >
|
|
(player.currentChapter?.duration ?? Duration.zero) -
|
|
positionChapter) {
|
|
Future.microtask(
|
|
() => throttlerEnd.call(() => player.skipToNext()),
|
|
);
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// dispose the timer
|
|
void dispose() {
|
|
for (var sub in _subscriptions) {
|
|
sub.cancel();
|
|
}
|
|
throttlerStart.dispose();
|
|
throttlerEnd.dispose();
|
|
// _playbackController.close();
|
|
}
|
|
}
|