Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Finish the book when skipping past the last chapter

Barnabas2aa0af1fa8b4
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 8e433e31..1ef76099 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -4256,7 +4256,7 @@ class AudioPlayerService extends ChangeNotifier {
bool _isCompletingBook = false;
- Future<void> _onPlaybackComplete() async {
+ Future<void> _onPlaybackComplete({bool userRequested = false}) async {
// Alpha: captures completion path choice for GH #186 (book restart bug).
// Re-entry attempts are logged too so we can see if completion fires
// multiple times from different signals (processingState, position-jump,
@@ -4274,7 +4274,7 @@ class AudioPlayerService extends ChangeNotifier {
// because the position-getter math (trackRel + offset[last]) lands at
// total duration. Recovery: seek a hair into the last track and resume,
// which forces AVPlayer to reload that single item correctly.
- if (Platform.isIOS && _trackStartOffsets.length > 2) {
+ if (!userRequested && Platform.isIOS && _trackStartOffsets.length > 2) {
final lastIdx = _trackStartOffsets.length - 2;
final lastTrackStart = _trackStartOffsets[lastIdx];
final lastTrackDur = _trackStartOffsets[lastIdx + 1] - lastTrackStart;
@@ -4309,7 +4309,9 @@ class AudioPlayerService extends ChangeNotifier {
// completion signal (iOS AVPlayer can fire completed on audio interruptions,
// buffer errors, etc.). Save current position and stop - don't mark finished
// or advance the queue.
- if (_totalDuration > 0 && _lastKnownPositionSec > 0 &&
+ if (!userRequested &&
+ _totalDuration > 0 &&
+ _lastKnownPositionSec > 0 &&
_lastKnownPositionSec < _totalDuration * 0.9 &&
_lastKnownPositionSec < _totalDuration - 30) {
debugPrint('[Player] Spurious completion at ${_lastKnownPositionSec.toStringAsFixed(1)}s / ${_totalDuration.toStringAsFixed(1)}s — saving position instead of marking finished');
@@ -5048,16 +5050,25 @@ class AudioPlayerService extends ChangeNotifier {
_resetStuckDetection();
if (!_player!.playing) _seekedWhilePaused = true;
final posS = position.inMilliseconds / 1000.0;
- for (int i = 0; i < _chapters.length; i++) {
- final start = (_chapters[i]['start'] as num?)?.toDouble() ?? 0;
- if (start > posS + 1.0) {
- debugPrint('[Service] skipToNextChapter → chapter $i at ${start}s');
- await _seekAbsolute(start);
- _logEvent(PlaybackEventType.seek, detail: 'next chapter');
- notifyListeners();
- return;
- }
+ final target = ChapterLookup.nextSkipTarget(
+ _chapters,
+ posS,
+ _totalDuration,
+ );
+ if (target == null) return;
+ if (target.finishesItem) {
+ debugPrint('[Service] skipToNextChapter → end at ${target.seconds}s');
+ _lastKnownPositionSec = target.seconds;
+ _logEvent(PlaybackEventType.seek, detail: 'next chapter to end');
+ await _onPlaybackComplete(userRequested: true);
+ return;
}
+ debugPrint(
+ '[Service] skipToNextChapter → ${target.seconds}s',
+ );
+ await _seekAbsolute(target.seconds);
+ _logEvent(PlaybackEventType.seek, detail: 'next chapter');
+ notifyListeners();
}
Future<void> skipToPreviousChapter() async {
diff --git a/lib/services/chapter_lookup.dart b/lib/services/chapter_lookup.dart
index 5fd5962b..794a74eb 100644
--- a/lib/services/chapter_lookup.dart
+++ b/lib/services/chapter_lookup.dart
@@ -32,4 +32,25 @@ class ChapterLookup {
}
return null;
}
+
+ static ({double seconds, bool finishesItem})? nextSkipTarget(
+ List<dynamic> chapters,
+ double positionSeconds,
+ double totalDuration,
+ ) {
+ if (chapters.isEmpty) return null;
+ for (final chapter in chapters) {
+ final map = chapter as Map<String, dynamic>;
+ final start = (map['start'] as num?)?.toDouble() ?? 0;
+ if (start > positionSeconds + 1.0) {
+ return (seconds: start, finishesItem: false);
+ }
+ }
+
+ final lastChapter = chapters.last as Map<String, dynamic>;
+ final lastEnd = (lastChapter['end'] as num?)?.toDouble() ?? 0;
+ final end = totalDuration > 0 ? totalDuration : lastEnd;
+ if (end <= 0) return null;
+ return (seconds: end, finishesItem: true);
+ }
}
diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dart
index 1d6e0c84..c2651037 100644
--- a/lib/services/chromecast_service.dart
+++ b/lib/services/chromecast_service.dart
@@ -6,6 +6,7 @@ import 'package:flutter/services.dart';
import 'package:flutter_chrome_cast/flutter_chrome_cast.dart';
import 'api_service.dart';
import 'audio_player_service.dart';
+import 'chapter_lookup.dart';
import 'progress_sync_service.dart';
enum CastConnectionState { disconnected, connecting, connected }
@@ -1036,10 +1037,19 @@ class ChromecastService extends ChangeNotifier {
Future<void> skipToNextChapter() async {
if (_castingChapters.isEmpty) return;
final p = _castPosition.inMilliseconds / 1000.0;
- for (final ch in _castingChapters) {
- final s = ((ch as Map)['start'] as num?)?.toDouble() ?? 0;
- if (s > p + 1.0) { await seekTo(Duration(milliseconds: (s * 1000).round())); return; }
+ final target = ChapterLookup.nextSkipTarget(
+ _castingChapters,
+ p,
+ _castingDuration,
+ );
+ if (target == null) return;
+ if (target.finishesItem) {
+ final itemId = _castingItemId;
+ await seekTo(Duration(milliseconds: (target.seconds * 1000).round()));
+ if (_castingItemId == itemId) await _onCastPlaybackComplete();
+ return;
}
+ await seekTo(Duration(milliseconds: (target.seconds * 1000).round()));
}
Future<void> skipToPreviousChapter() async {