Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix multi-channel audio crash, dedupe absorbing IDs, version bump

Nathan Poulson54d657847ae0
diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dart
index adb9402a..8f6a7c7e 100644
--- a/lib/providers/library_provider.dart
+++ b/lib/providers/library_provider.dart
@@ -1699,6 +1699,17 @@ class LibraryProvider extends ChangeNotifier {
Set<String> get manualAbsorbRemoves => _manualAbsorbRemoves;
List<String> get absorbingBookIds => _absorbingBookIds;
+ bool _dedupeAbsorbingIds() {
+ final seen = <String>{};
+ final deduped = <String>[];
+ for (final key in _absorbingBookIds) {
+ if (seen.add(key)) deduped.add(key);
+ }
+ if (deduped.length == _absorbingBookIds.length) return false;
+ _absorbingBookIds = deduped;
+ return true;
+ }
+
/// Move an existing absorbing key to the front and persist.
/// No-op if the key isn't in the list or is already first.
void moveAbsorbingToFront(String key) {
@@ -1719,10 +1730,12 @@ class LibraryProvider extends ChangeNotifier {
/// [afterKey] — insert right after this key (e.g. next-in-series after
/// the finished book). Repositions even if already in list.
void _absorbingIdsAdd(String key, {String? afterKey, bool atFront = true}) {
+ if (_absorbingBookIds.contains(key) && afterKey == null) return;
+
if (afterKey != null) {
+ _absorbingBookIds.remove(key);
final afterIdx = _absorbingBookIds.indexOf(afterKey);
if (afterIdx >= 0) {
- _absorbingBookIds.remove(key);
_absorbingBookIds.insert(afterIdx + 1, key);
return;
}
@@ -1757,9 +1770,13 @@ class LibraryProvider extends ChangeNotifier {
if (key != null) _absorbingItemCache[key] = m;
} catch (_) {}
}
+ if (_dedupeAbsorbingIds()) {
+ await _saveManualAbsorbing();
+ }
}
Future<void> _saveManualAbsorbing() async {
+ _dedupeAbsorbingIds();
await ScopedPrefs.setStringList(
'absorbing_manual_adds', _manualAbsorbAdds.toList());
await ScopedPrefs.setStringList(
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index 443c5e52..3ef9624b 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -45,6 +45,8 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
@override
void initState() {
super.initState();
+ _lastSeenHasBook = _player.hasBook;
+ _lastSeenIsPlaying = _player.isPlaying;
_player.addListener(_rebuild);
_cast.addListener(_rebuild);
_restoreLastFinished();
@@ -83,10 +85,18 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
// slide to the beginning rather than the list instantly reordering underneath them.
bool _suppressReorder = false;
bool _mergeLibraries = false;
+ bool? _lastSeenHasBook;
+ bool? _lastSeenIsPlaying;
void _rebuild() {
if (!mounted) return;
- _loadMergeLibraries(); // refresh in case setting changed
+
+ final hasBookChanged = _player.hasBook != _lastSeenHasBook;
+ final isPlayingChanged = _player.isPlaying != _lastSeenIsPlaying;
+ _lastSeenHasBook = _player.hasBook;
+ _lastSeenIsPlaying = _player.isPlaying;
+ var shouldRebuild = hasBookChanged || isPlayingChanged;
+
// Detect item or episode change (same show, different episode counts as a change)
final itemChanged = _player.currentItemId != _lastPlayingId;
final episodeChanged = _player.currentEpisodeId != _lastPlayingEpisodeId;
@@ -131,6 +141,7 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
_lastFinishedId = finishedKey;
ScopedPrefs.setString('absorbing_last_finished', finishedKey);
}
+ shouldRebuild = true;
}
// Track cast state — when casting starts, scroll to the card;
@@ -153,9 +164,12 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
_lastCastItemId = null;
_lastCastEpisodeId = null;
}
+ final castChanged = nowCasting != _wasCasting;
_wasCasting = nowCasting;
- setState(() {});
+ if (shouldRebuild || castChanged) {
+ setState(() {});
+ }
}
void _scrollToActiveCard({int retries = 2}) {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 2ab1417c..6c22af7a 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -438,15 +438,33 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {
/// Subscribe to the player's playback event stream and forward state to
/// the system MediaSession. If the stream errors or completes unexpectedly,
/// re-subscribe so system media controls stay alive.
+ /// Rate-limited to prevent infinite error loops (e.g. multi-channel audio).
+ int _resubscribeCount = 0;
+ DateTime _lastResubscribe = DateTime.now();
+
void _subscribePlaybackEvents() {
_player.playbackEventStream.map(_transformEvent).listen(
- playbackState.add,
+ (state) {
+ playbackState.add(state);
+ // Reset error counter on successful events
+ _resubscribeCount = 0;
+ },
onError: (Object e, StackTrace st) {
- debugPrint('[Player] playbackEvent error - re-subscribing: $e');
- // Push the last known good state so the notification doesn't go stale
- refreshPlaybackState();
- // Re-subscribe so future events still reach the MediaSession
- _subscribePlaybackEvents();
+ final now = DateTime.now();
+ // Reset counter if it's been more than 5 seconds since last error
+ if (now.difference(_lastResubscribe).inSeconds > 5) {
+ _resubscribeCount = 0;
+ }
+ _lastResubscribe = now;
+ _resubscribeCount++;
+
+ if (_resubscribeCount <= 3) {
+ debugPrint('[Player] playbackEvent error ($_resubscribeCount/3) - re-subscribing: $e');
+ refreshPlaybackState();
+ _subscribePlaybackEvents();
+ } else {
+ debugPrint('[Player] playbackEvent error - too many rapid failures, stopping re-subscribe: $e');
+ }
},
onDone: () {
debugPrint('[Player] playbackEvent stream completed - re-subscribing');
diff --git a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java
index 8e828917..2477ffe1 100644
--- a/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java
+++ b/packages/just_audio/android/src/main/java/com/ryanheise/just_audio/AudioPlayer.java
@@ -870,6 +870,33 @@ public class AudioPlayer implements MethodCallHandler, Player.Listener, Metadata
// Upmix mono sources to stereo so Android AudioTrack handles them reliably
monoProcessor.putChannelMixingMatrix(
new androidx.media3.common.audio.ChannelMixingMatrix(1, 2, new float[]{1f, 1f}));
+ // Downmix 5.1 surround (6ch) to stereo so devices that can't output
+ // multi-channel PCM don't crash. Standard ITU-R BS.775 coefficients:
+ // L = FL + 0.707*FC + 0.707*SL + 0.5*LFE
+ // R = FR + 0.707*FC + 0.707*SR + 0.5*LFE
+ // Channel order: FL, FR, FC, LFE, SL, SR
+ monoProcessor.putChannelMixingMatrix(
+ new androidx.media3.common.audio.ChannelMixingMatrix(6, 2, new float[]{
+ 1.0f, 0.0f, // FL -> L, R
+ 0.0f, 1.0f, // FR -> L, R
+ 0.707f, 0.707f, // FC -> L, R
+ 0.5f, 0.5f, // LFE -> L, R
+ 0.707f, 0.0f, // SL -> L, R
+ 0.0f, 0.707f // SR -> L, R
+ }));
+ // Downmix 7.1 surround (8ch) to stereo
+ // Channel order: FL, FR, FC, LFE, SL, SR, BL, BR
+ monoProcessor.putChannelMixingMatrix(
+ new androidx.media3.common.audio.ChannelMixingMatrix(8, 2, new float[]{
+ 1.0f, 0.0f, // FL -> L, R
+ 0.0f, 1.0f, // FR -> L, R
+ 0.707f, 0.707f, // FC -> L, R
+ 0.5f, 0.5f, // LFE -> L, R
+ 0.707f, 0.0f, // SL -> L, R
+ 0.0f, 0.707f, // SR -> L, R
+ 0.5f, 0.0f, // BL -> L, R
+ 0.0f, 0.5f // BR -> L, R
+ }));
sMonoProcessor = monoProcessor;
MonoController.register(AudioPlayer::setMonoEnabled);
RenderersFactory renderersFactory = new DefaultRenderersFactory(context) {
diff --git a/pubspec.yaml b/pubspec.yaml
index c8a734ae..a1911668 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: A Material 3 Audiobookshelf client.
publish_to: 'none'
-version: 1.8.20+48
+version: 1.8.23+50
environment:
sdk: '>=3.1.0 <4.0.0'