Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a Playback setting to keep playing when another app starts its own audio

Barnabasa0e060071fb7
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
index 2ed4bb6c..859cf36e 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/MainActivity.kt
@@ -101,6 +101,11 @@ class MainActivity : AudioServiceActivity() {
"isBluetoothAudioConnected" -> {
result.success(isBluetoothAudioConnected())
}
+ // 0 normal, 1 ringing, 2 in a call, 3 in a VoIP call.
+ "audioMode" -> {
+ val am = getSystemService(Context.AUDIO_SERVICE) as AudioManager
+ result.success(am.mode)
+ }
"init" -> handleInit(result)
"attachSession" -> {
val sessionId = call.argument<Int>("sessionId") ?: 0
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index d6619987..dfd1421e 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -238,6 +238,7 @@ class SettingsScreenState extends State<SettingsScreen> {
bool _notifChapterProgress = false;
bool _notifSpeedBookmark = false;
bool _duckBriefInterruptions = false;
+ bool _keepPlayingWithOtherApps = false;
bool _autoplayOnCarConnect = false;
bool _keepScreenOnPlayer = false;
bool _keepScreenOnCarMode = false;
@@ -1382,6 +1383,7 @@ class SettingsScreenState extends State<SettingsScreen> {
final podcastTabLibraryId = await PlayerSettings.getPodcastTabLibraryId();
final episodeNotifMinutes = await PlayerSettings.getEpisodeNotifIntervalMinutes();
final duckBriefInterruptions = await PlayerSettings.getDuckBriefInterruptions();
+ final keepPlayingWithOtherApps = await PlayerSettings.getKeepPlayingWithOtherApps();
final autoplayOnCarConnect = await PlayerSettings.getAutoplayOnCarConnect();
final keepScreenOnPlayer = await PlayerSettings.getKeepScreenOnInPlayer();
final keepScreenOnCarMode = await PlayerSettings.getKeepScreenOnInCarMode();
@@ -1408,6 +1410,7 @@ class SettingsScreenState extends State<SettingsScreen> {
_notifChapterProgress = notifChapter;
_notifSpeedBookmark = notifSpeedBookmark;
_duckBriefInterruptions = duckBriefInterruptions;
+ _keepPlayingWithOtherApps = keepPlayingWithOtherApps;
_autoplayOnCarConnect = autoplayOnCarConnect;
_keepScreenOnPlayer = keepScreenOnPlayer;
_keepScreenOnCarMode = keepScreenOnCarMode;
@@ -3544,6 +3547,20 @@ class SettingsScreenState extends State<SettingsScreen> {
} : null,
),
const Divider(height: 1, indent: 16, endIndent: 16),
+ SwitchListTile(
+ title: const Text('Play with other audio'),
+ subtitle: Text(
+ _keepPlayingWithOtherApps
+ ? 'Keeps playing when another app starts its own audio'
+ : 'Pauses when another app starts its own audio',
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _keepPlayingWithOtherApps,
+ onChanged: _loaded ? (v) {
+ setState(() => _keepPlayingWithOtherApps = v);
+ PlayerSettings.setKeepPlayingWithOtherApps(v);
+ } : null,
+ ),
+ const Divider(height: 1, indent: 16, endIndent: 16),
SwitchListTile(
title: Text(l.speedBookmarkInControls),
subtitle: Text(
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 2eeea862..6b03d8f7 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1721,6 +1721,9 @@ class AudioPlayerService extends ChangeNotifier {
}
await _configureAudioSession();
});
+ PlayerSettings.getKeepPlayingWithOtherApps().then((v) {
+ _keepPlayingWithOtherApps = v;
+ });
}
}
@@ -3112,6 +3115,7 @@ class AudioPlayerService extends ChangeNotifier {
// Whether BT audio was connected when the current interruption began.
static bool _wasOnBluetooth = false;
static bool _duckBriefInterruptions = false;
+ static bool _keepPlayingWithOtherApps = false;
static double? _volumeBeforeInterruptionDuck;
// Safety net for a duck whose matching "end" event never arrives - a known
// flaky spot on Android, more so over Bluetooth where focus changes route
@@ -3178,12 +3182,27 @@ class AudioPlayerService extends ChangeNotifier {
/// starting new playback to avoid blasting audio on the phone speaker.
static bool get wasNoisyPause => _noisyPause;
+ /// Whether the phone is ringing or in a call (including VoIP), read from
+ /// the audio mode, which needs no permission. False where unknown.
+ static Future<bool> _isInCall() async {
+ if (!AppPlatform.isAndroid) return false;
+ try {
+ final mode = await _eqChannelForDiag.invokeMethod<int>('audioMode');
+ return mode != null && mode != 0;
+ } catch (_) {
+ return false;
+ }
+ }
+
static Future<void> _configureAudioSession() async {
if (AppPlatform.lacksPhonePlugins) return;
final session = await AudioSession.instance;
_duckBriefInterruptions =
AppPlatform.isAndroid &&
await PlayerSettings.getDuckBriefInterruptions();
+ _keepPlayingWithOtherApps =
+ AppPlatform.isAndroid &&
+ await PlayerSettings.getKeepPlayingWithOtherApps();
await session.configure(
AudioSessionConfiguration(
@@ -3242,6 +3261,19 @@ class AudioPlayerService extends ChangeNotifier {
_clearDuckWatchdog();
await service._player?.setVolume(duckedVolume);
}
+ // Android reports another app taking the audio for good as
+ // `unknown`, and on a Pixel an incoming call arrives the same
+ // way, so the phone's audio mode tells the two apart. With the
+ // setting on, another app is the one case to play through.
+ final lossType = AppPlatform.isAndroid &&
+ event.type == AudioInterruptionType.unknown;
+ final inCall = lossType && await _isInCall();
+ if (_keepPlayingWithOtherApps && lossType && !inCall) {
+ debugPrint(
+ '[AudioSession] Another app took the audio - keeping playing',
+ );
+ return;
+ }
if (service.isPlaying) {
debugPrint(
'[AudioSession] Interrupted (${event.type}) — pausing',
@@ -3264,8 +3296,9 @@ class AudioPlayerService extends ChangeNotifier {
// and nav prompts come as transient `pause`/`duck`. Only a
// transient one should come back by itself - resuming after a
// permanent one talked over the radio the user switched to.
- final permanentLoss = AppPlatform.isAndroid &&
- event.type == AudioInterruptionType.unknown;
+ // A call that came in as a permanent loss is still a call:
+ // it ends, and the book should come back like any other.
+ final permanentLoss = lossType && !inCall;
service._wasPlayingBeforeInterrupt = !permanentLoss;
if (permanentLoss) {
debugPrint(
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index 241f972f..fe2bcd64 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -1030,6 +1030,13 @@ class PlayerSettings {
static Future<void> setDuckBriefInterruptions(bool value) =>
_set('duckBriefInterruptions', value, notify: true);
+ // Android only: keep playing when another app starts its own audio, instead
+ // of stopping for it. Calls and navigation prompts still pause or duck.
+ static Future<bool> getKeepPlayingWithOtherApps() =>
+ _get('keepPlayingWithOtherApps', false);
+ static Future<void> setKeepPlayingWithOtherApps(bool value) =>
+ _set('keepPlayingWithOtherApps', value, notify: true);
+
// GH #371: start the last played book when the car (Android Auto or
// CarPlay) connects and nothing is loaded yet. The warm case (session
// alive but paused) belongs to the car system's own resume behavior; this