Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix the cast reconnect stalling, its missing permission, and messy stream blip recovery

Barnabas9507929f73b2
diff --git a/android/app/src/fdroid/AndroidManifest.xml b/android/app/src/fdroid/AndroidManifest.xml
index 61a91e11..b5f772e5 100644
--- a/android/app/src/fdroid/AndroidManifest.xml
+++ b/android/app/src/fdroid/AndroidManifest.xml
@@ -10,6 +10,7 @@
<!-- Only needed for the Cast SDK's own reconnection service, below -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE" tools:node="remove"/>
+ <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" tools:node="remove"/>
<application>
<!-- Chromecast (GMS) -->
diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index e1172e9e..da791b69 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -7,6 +7,12 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
+ <!-- Android 14+ only allows a connectedDevice foreground service to start
+ when the app also holds one of a short list of device-adjacent
+ permissions. This is the least invasive of them: install-time, no
+ user prompt, and cast discovery is mDNS multicast anyway. Needed so
+ the Cast SDK's ReconnectionService (below) can actually run. -->
+ <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 83180dc9..339a0010 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -4612,6 +4612,12 @@ class AudioPlayerService extends ChangeNotifier {
return;
}
+ // A stream error usually means the cached-session URLs went bad (expired
+ // baked token, dead public session). Drop the cache entry so the rebuild
+ // takes the fresh-session path - tokenless public URLs with a
+ // known-good credential - instead of re-baking whatever just failed.
+ await SessionCache.clear(itemId: itemId, episodeId: episodeId);
+
debugPrint('[Player] Retrying playback at ${retryPos.toStringAsFixed(1)}s');
final ok = await playItem(
api: api,
diff --git a/lib/services/chromecast_service.dart b/lib/services/chromecast_service.dart
index 337cff77..81034f0a 100644
--- a/lib/services/chromecast_service.dart
+++ b/lib/services/chromecast_service.dart
@@ -284,7 +284,13 @@ class ChromecastService extends ChangeNotifier {
_reconnectTimer = Timer(delay, () async {
if (!_reconnecting || isConnected) return;
try {
- await GoogleCastSessionManager.instance.endSession();
+ // The plugin's Android handler runs endSession but never posts a
+ // MethodChannel reply, so awaiting it bare hangs forever. The end
+ // itself takes effect immediately; the timeout just keeps this chain
+ // moving whether or not a reply ever shows up.
+ await GoogleCastSessionManager.instance
+ .endSession()
+ .timeout(const Duration(milliseconds: 500));
} catch (_) {}
if (!_reconnecting) return;
try {