Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add error messages for playback failures (closes #60)

Nathan Poulsonbe6db7c9a675
diff --git a/lib/screens/bookmarks_screen.dart b/lib/screens/bookmarks_screen.dart
index 1936c607..0e51f797 100644
--- a/lib/screens/bookmarks_screen.dart
+++ b/lib/screens/bookmarks_screen.dart
@@ -7,6 +7,7 @@ import '../providers/library_provider.dart';
import '../screens/app_shell.dart';
import '../services/audio_player_service.dart';
import '../services/bookmark_service.dart';
+import '../widgets/card_buttons.dart';
import '../services/download_service.dart';
import '../widgets/absorb_page_header.dart';
@@ -207,7 +208,7 @@ class _BookmarksScreenState extends State<BookmarksScreen> {
? (media['duration'] as num).toDouble() : 0.0;
final chapters = (media['chapters'] as List<dynamic>?) ?? [];
- await player.playItem(
+ final error = await player.playItem(
api: api,
itemId: itemId,
title: title,
@@ -217,6 +218,7 @@ class _BookmarksScreenState extends State<BookmarksScreen> {
chapters: chapters,
startTime: bookmark.positionSeconds,
);
+ if (error != null && mounted) showErrorSnackBar(context, error);
if (mounted) Navigator.pop(context);
AppShell.goToAbsorbingGlobal();
diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart
index 2d62e30c..44cbff1a 100644
--- a/lib/screens/home_screen.dart
+++ b/lib/screens/home_screen.dart
@@ -9,6 +9,7 @@ import '../widgets/home_section.dart';
import '../widgets/absorb_page_header.dart';
import '../widgets/shimmer.dart';
import '../widgets/book_detail_sheet.dart';
+import '../widgets/card_buttons.dart';
import '../widgets/episode_list_sheet.dart';
import 'app_shell.dart';
@@ -712,13 +713,16 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
final epDuration = (recentEpisode['duration'] as num?)?.toDouble() ?? 0;
final coverUrl = widget.lib.getCoverUrl(itemId);
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: itemId, title: episodeTitle, author: showTitle,
coverUrl: coverUrl, totalDuration: epDuration, chapters: [],
episodeId: episodeId,
episodeTitle: episodeTitle,
);
- if (mounted) setState(() => _isLoading = false);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ setState(() => _isLoading = false);
+ }
return;
}
@@ -736,10 +740,11 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {
final chapters = (media['chapters'] as List<dynamic>?) ?? [];
// Start playback
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: itemId, title: title, author: author,
coverUrl: coverUrl, totalDuration: duration, chapters: chapters,
);
+ if (error != null && mounted) showErrorSnackBar(context, error);
// Ensure this book is on the absorbing list (clear any manual remove)
if (context.mounted) {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index a99956a8..cb08572f 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1171,7 +1171,7 @@ class AudioPlayerService extends ChangeNotifier {
String? _currentEpisodeTitle;
String? get currentEpisodeTitle => _currentEpisodeTitle;
- Future<bool> playItem({
+ Future<String?> playItem({
required ApiService api,
required String itemId,
required String title,
@@ -1189,7 +1189,7 @@ class AudioPlayerService extends ChangeNotifier {
}
if (_handler == null) {
debugPrint('[Player] Handler init failed, cannot play');
- return false;
+ return 'Player failed to initialize';
}
// Stop Chromecast if currently casting
@@ -1246,7 +1246,7 @@ class AudioPlayerService extends ChangeNotifier {
_isCompletingBook = false;
// Check if downloaded — play locally
- final bool result;
+ final String? result;
if (_downloadService.isDownloaded(progressKey)) {
result = await _playFromLocal(progressKey, title, author, coverUrl,
totalDuration, chapters, startTime);
@@ -1257,7 +1257,7 @@ class AudioPlayerService extends ChangeNotifier {
if (manualOffline) {
debugPrint('[Player] Manual offline — cannot stream non-downloaded item');
_clearState();
- return false;
+ return 'This item isn\'t downloaded and offline mode is on';
}
// Stream from server
result = await _playFromServer(api, itemId, title, author, coverUrl,
@@ -1265,7 +1265,7 @@ class AudioPlayerService extends ChangeNotifier {
}
// Auto-navigate to Absorbing tab when an episode starts playing
- if (result && episodeId != null) {
+ if (result == null && episodeId != null) {
_onEpisodePlayStartedCallback?.call();
}
return result;
@@ -1338,7 +1338,7 @@ class AudioPlayerService extends ChangeNotifier {
}
}
- Future<bool> _playFromLocal(
+ Future<String?> _playFromLocal(
String itemId,
String title,
String author,
@@ -1418,7 +1418,7 @@ class AudioPlayerService extends ChangeNotifier {
if (localPaths == null || localPaths.isEmpty) {
debugPrint('[Player] No local files found');
_clearState();
- return false;
+ return 'Downloaded files not found - try re-downloading';
}
// Get cached session data for track durations (and chapters if needed)
@@ -1482,15 +1482,15 @@ class AudioPlayerService extends ChangeNotifier {
final sleepTimer = SleepTimerService();
sleepTimer.resetDismiss();
sleepTimer.checkAutoSleep();
- return true;
+ return null;
} catch (e, stack) {
debugPrint('[Player] Local play error: $e\n$stack');
_clearState();
- return false;
+ return 'Playback failed: ${e.toString().split('\n').first}';
}
}
- Future<bool> _playFromServer(
+ Future<String?> _playFromServer(
ApiService api,
String itemId,
String title,
@@ -1510,14 +1510,14 @@ class AudioPlayerService extends ChangeNotifier {
if (sessionData == null) {
debugPrint('[Player] Failed to start playback session');
_clearState();
- return false;
+ return 'Could not connect to server';
}
_playbackSessionId = sessionData['id'] as String?;
final audioTracks = sessionData['audioTracks'] as List<dynamic>?;
if (audioTracks == null || audioTracks.isEmpty) {
_clearState();
- return false;
+ return 'No audio files found - this item may be missing on the server';
}
// Pick up chapters from session (e.g. podcast episodes with embedded chapters)
@@ -1601,11 +1601,11 @@ class AudioPlayerService extends ChangeNotifier {
final sleepTimer = SleepTimerService();
sleepTimer.resetDismiss();
sleepTimer.checkAutoSleep();
- return true;
+ return null;
} catch (e, stack) {
debugPrint('[Player] Stream error: $e\n$stack');
_clearState();
- return false;
+ return 'Playback failed: ${e.toString().split('\n').first}';
}
}
@@ -1749,10 +1749,10 @@ class AudioPlayerService extends ChangeNotifier {
);
_retryInProgress = false;
- if (ok) {
+ if (ok == null) {
debugPrint('[Player] Retry succeeded');
} else {
- debugPrint('[Player] Retry failed');
+ debugPrint('[Player] Retry failed: $ok');
}
}
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 95e55e88..5fec00b2 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -841,13 +841,16 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
final auth = context.read<AuthProvider>();
final api = auth.apiService;
if (api == null) { setState(() => _isStarting = false); return; }
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: _itemId, title: _title, author: _author,
coverUrl: _coverUrl, totalDuration: _effectiveDuration, chapters: _chapters,
episodeId: _episodeId,
episodeTitle: _recentEpisode?['title'] as String?,
);
- if (mounted) setState(() => _isStarting = false);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ setState(() => _isStarting = false);
+ }
}
Future<void> _listenAgain() async {
@@ -870,14 +873,17 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
// Clear the locally saved position so playItem() doesn't override startTime
// back to the end (where it was saved on completion)
await ProgressSyncService().deleteLocal(progressKey);
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: _itemId, title: _title, author: _author,
coverUrl: _coverUrl, totalDuration: _episodeId != null ? _effectiveDuration : _duration,
chapters: _chapters,
episodeId: _episodeId,
episodeTitle: _recentEpisode?['title'] as String?,
);
- if (mounted) setState(() => _isStarting = false);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ setState(() => _isStarting = false);
+ }
}
void _deleteDownload(String dlKey) {
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index 0b1aff7b..2533a350 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -13,6 +13,7 @@ import 'package:url_launcher/url_launcher.dart';
import '../providers/auth_provider.dart';
import '../providers/library_provider.dart';
import '../services/audio_player_service.dart';
+import 'card_buttons.dart';
import '../services/api_service.dart';
import '../services/download_service.dart';
import '../services/progress_sync_service.dart';
@@ -707,7 +708,8 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
if (api == null) return;
// Start playback
- await player.playItem(api: api, itemId: widget.itemId, title: title, author: author, coverUrl: coverUrl, totalDuration: duration, chapters: chapters);
+ final error = await player.playItem(api: api, itemId: widget.itemId, title: title, author: author, coverUrl: coverUrl, totalDuration: duration, chapters: chapters);
+ if (error != null && context.mounted) showErrorSnackBar(context, error);
// Refresh library so the absorbing screen picks up the new book
if (context.mounted) {
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index 2dfef235..0d560c29 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -18,6 +18,17 @@ void showInactiveToast(BuildContext context) {
));
}
+/// Show an error message to the user.
+void showErrorSnackBar(BuildContext context, String message) {
+ ScaffoldMessenger.of(context)
+ ..clearSnackBars()
+ ..showSnackBar(SnackBar(
+ content: Text(message),
+ duration: const Duration(seconds: 4),
+ behavior: SnackBarBehavior.floating,
+ ));
+}
+
// ─── WIDE GLASS BUTTON (for 2-column grid) ─────────────────
class CardWideButton extends StatelessWidget {
diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dart
index 15c7cabd..3ae4a826 100644
--- a/lib/widgets/episode_list_sheet.dart
+++ b/lib/widgets/episode_list_sheet.dart
@@ -8,6 +8,7 @@ import '../services/download_service.dart';
import '../services/progress_sync_service.dart';
import '../services/chromecast_service.dart';
import '../providers/auth_provider.dart';
+import 'card_buttons.dart';
import 'html_description.dart';
/// Bottom sheet that shows a podcast's episode list.
@@ -154,7 +155,7 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
}
final player = AudioPlayerService();
- await player.playItem(
+ final error = await player.playItem(
api: api,
itemId: _itemId,
title: episodeTitle,
@@ -165,7 +166,10 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {
episodeId: episodeId,
episodeTitle: episodeTitle,
);
- if (mounted) Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ }
}
Future<void> _downloadEpisode(Map<String, dynamic> episode) async {
@@ -537,13 +541,16 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {
return;
}
- await AudioPlayerService().playItem(
+ final error = await AudioPlayerService().playItem(
api: api, itemId: _itemId, title: _episodeTitle, author: _showTitle,
coverUrl: api.getCoverUrl(_itemId), totalDuration: _duration, chapters: _chapters,
episodeId: _episodeId,
episodeTitle: _episodeTitle,
);
- if (mounted) Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ Navigator.of(context, rootNavigator: true).popUntil((route) => route.isFirst);
+ }
}
Future<void> _download() async {
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index f394a2db..7f26c77c 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -952,13 +952,16 @@ class _ExpandedCardState extends State<ExpandedCard> {
final auth = context.read<AuthProvider>();
final api = auth.apiService;
if (api == null) { setState(() => _isStarting = false); return; }
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: _itemId, title: _title, author: _author,
coverUrl: _coverUrl, totalDuration: _effectiveDuration, chapters: _chapters,
episodeId: _episodeId,
episodeTitle: _recentEpisode?['title'] as String?,
);
- if (mounted) setState(() => _isStarting = false);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ setState(() => _isStarting = false);
+ }
}
Future<void> _listenAgain() async {
@@ -973,11 +976,14 @@ class _ExpandedCardState extends State<ExpandedCard> {
await api.resetProgress(_itemId, _duration);
lib.resetProgressFor(_itemId);
await ProgressSyncService().deleteLocal(_itemId);
- await widget.player.playItem(
+ final error = await widget.player.playItem(
api: api, itemId: _itemId, title: _title, author: _author,
coverUrl: _coverUrl, totalDuration: _duration, chapters: _chapters,
);
- if (mounted) setState(() => _isStarting = false);
+ if (mounted) {
+ if (error != null) showErrorSnackBar(context, error);
+ setState(() => _isStarting = false);
+ }
}
Future<void> _removeFromAbsorbing() async {