Refactor chapter seeking logic in AudiobookPlayerSeekChapterButton

This commit is contained in:
Dr-Blank 2024-05-19 10:02:39 -04:00
parent 74dc9807d6
commit 2f078742d0
No known key found for this signature in database
GPG key ID: 7452CC63F210A266

View file

@ -311,7 +311,19 @@ class AudiobookPlayerSeekChapterButton extends HookConsumerWidget {
return;
}
if (isForward) {
player.seek(player.currentChapter!.end);
// instead of seeking to the end of the chapter, go to the next chapter start
// player.seek(player.currentChapter!.end);
final index = player.book!.chapters.indexOf(player.currentChapter!);
if (index < player.book!.chapters.length - 1) {
player.seek(
player.book!.chapters[index + 1].start +
const Duration(
microseconds: 50,
), // add a small offset so the display does not show the previous chapter for a split second
);
} else {
player.seek(player.currentChapter!.end);
}
} else {
// if player position is less than 5 seconds into the chapter, go to the previous chapter
final chapterPosition =
@ -319,10 +331,15 @@ class AudiobookPlayerSeekChapterButton extends HookConsumerWidget {
if (chapterPosition < const Duration(seconds: 5)) {
final index = player.book!.chapters.indexOf(player.currentChapter!);
if (index > 0) {
player.seek(player.book!.chapters[index - 1].start);
player.seek(
player.book!.chapters[index - 1].start +
const Duration(microseconds: 50),
);
}
} else {
player.seek(player.currentChapter!.start);
player.seek(
player.currentChapter!.start + const Duration(microseconds: 50),
);
}
}
},