A cross platform Audiobookshelf client for Android and iOS
Dart
Rebuild the player after a long pause so play no longer comes back silent, and log the audio route, the last app exit and player errors
Barnabas83edb8c31dfe
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 5350b514..1e074045 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -114,7 +114,8 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {'keyCode=${snap['lastKeyCode']} keyAgeMs=${snap['lastKeyAgeMs']} ''lastPlayCaller=${snap['lastPlayCaller']} playAgeMs=${snap['lastPlayCallerAgeMs']} ''lastPauseCaller=${snap['lastPauseCaller']} pauseAgeMs=${snap['lastPauseCallerAgeMs']} '- 'carClientAgeMs=${snap['carClientAgeMs']} keyPkg=${snap['lastKeyPkg']}',+ 'carClientAgeMs=${snap['carClientAgeMs']} keyPkg=${snap['lastKeyPkg']} '+ 'route=[${snap['route']}] outputs=[${snap['outputs']}]',);}@@ -305,7 +306,12 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {'[Player] playbackEvent error - too many rapid failures, stopping re-subscribe: $e',);if (PlaybackErrorPolicy.shouldRetryWithTranscode(e)) {- AudioPlayerService()._retryWithTranscode();+ final service = AudioPlayerService();+ if (service._localSessionMode) {+ service._recoverLocalPlayback(e);+ } else {+ service._retryWithTranscode();+ }}}},@@ -2543,6 +2549,14 @@ class AudioPlayerService extends ChangeNotifier {/// we can tell apart "player thinks it's playing but no audio reaches/// speakers" from "player is buffering forever" from "wrong output/// route" etc.+ /// The system's record of how the last process ended, for deaths the log+ /// itself cannot see coming.+ static Future<void> _reportPreviousExit() async {+ final snap = await AudioPlayerHandler._absorbDiagSnapshot();+ final info = snap?['lastExit'];+ if (info != null) debugPrint('[Init] Previous process exit: $info');+ }+Future<void> _logAudioDiagnostics(String stage) async {try {final p = _player;@@ -2562,6 +2576,18 @@ class AudioPlayerService extends ChangeNotifier {'ep=$_currentEpisodeId',];+ if (Platform.isAndroid) {+ // Which output the sound is actually leaving through. A player that+ // advances with nothing audible and a Bluetooth route here means the+ // audio went to a device the listener was not wearing.+ final snap = await AudioPlayerHandler._absorbDiagSnapshot();+ if (snap != null) {+ pieces.add('route=[${snap['route']}]');+ pieces.add('outputs=[${snap['outputs']}]');+ pieces.add('musicVol=${snap['musicVolume']}');+ }+ }+if (Platform.isIOS) {try {final info = await _eqChannelForDiag@@ -2918,6 +2944,7 @@ class AudioPlayerService extends ChangeNotifier {_handler!._cachedLockSeekBar = await PlayerSettings.getLockSeekBar();debugPrint('[Player] AudioService initialized');if (Platform.isIOS) unawaited(_reportPreviousBackgroundDeath());+ if (Platform.isAndroid) unawaited(_reportPreviousExit());unawaited(TranscriptionService.instance.reportInterruptedRun());// Configure streaming cache if enabledfinal cacheSizeMb = await PlayerSettings.getStreamingCacheSizeMb();@@ -5186,6 +5213,44 @@ class AudioPlayerService extends ChangeNotifier {bool _transcodeRetryInFlight = false;+ // Set when the player failed while paused; the next play rebuilds it+ // instead of trusting its position or its decoder.+ bool _playerBroken = false;++ /// A downloaded book needs no server to come back from a decoder failure.+ /// Android can take the decoder away from a paused app, which surfaces as+ /// a renderer error; the file is fine. Playing: start over where it was.+ /// Paused: leave it alone and rebuild on the next play, from the saved+ /// position, so the error can't send the book back to the beginning.+ Future<void> _recoverLocalPlayback(Object e) async {+ if (_currentItemId == null || _api == null) return;+ final wasPlaying = _player?.playing ?? false;+ final startS = position.inMilliseconds / 1000.0;+ debugPrint(+ '[Player] Decoder failed on a local book at ${startS.toStringAsFixed(1)}s '+ '(playing=$wasPlaying) - '+ '${wasPlaying ? "restarting from there" : "rebuilding on the next play"}: $e',+ );+ if (!wasPlaying) {+ _playerBroken = true;+ return;+ }+ await playItem(+ api: _api!,+ itemId: _currentItemId!,+ title: _currentTitle ?? '',+ author: _currentAuthor ?? '',+ coverUrl: _currentCoverUrl,+ totalDuration: _totalDuration,+ chapters: _chapters,+ startTime: startS,+ forceStartTime: startS > 0,+ episodeId: _currentEpisodeId,+ episodeTitle: _currentEpisodeTitle,+ libraryId: _currentLibraryId,+ );+ }+Future<void> _retryWithTranscode() async {if (_transcodeRetryInFlight) return;_transcodeRetryInFlight = true;@@ -7020,7 +7085,10 @@ class AudioPlayerService extends ChangeNotifier {settings.maxRewind,activationDelay: settings.activationDelay,);- if (rewindSeconds > 0.5) {+ // A player sitting at zero has lost its place (an error reset it);+ // rewinding from there would only seek to the start on top of+ // whatever the server check just restored.+ if (rewindSeconds > 0.5 && position > Duration.zero) {final currentAbsolutePos = position.inMilliseconds / 1000.0;final currentSpeed = _player!.speed;var newPosSeconds =@@ -7052,16 +7120,64 @@ class AudioPlayerService extends ChangeNotifier {}}}+ final pausedFor = _lastPauseTime == null+ ? null+ : DateTime.now().difference(_lastPauseTime!);_lastPauseTime = null;// Reset server sync clock so the first sync after resume doesn't// include pause duration as timeListened_lastServerSync = DateTime.now();_lastAccrual = DateTime.now();_lastAccrualPos = null;- // Re-activate audio session in case a prior stop released it.+ // Re-activate audio session in case a prior stop released it. The answer+ // is the audio focus request: a refusal here on a resume from the+ // background is a lead when the book then plays without sound.try {- (await AudioSession.instance).setActive(true);- } catch (_) {}+ final activated = await (await AudioSession.instance).setActive(true);+ debugPrint('[Player] Resume setActive(true)=$activated '+ '(paused ${pausedFor?.inSeconds ?? '?'}s)');+ } catch (e) {+ debugPrint('[Player] Resume setActive(true) failed: $e');+ }+ // Android: a player left paused past the pause timeout comes back with+ // the position moving and nothing audible, and only a rebuilt player+ // sounds again. Rebuild it at the position the resume settled on, which+ // already includes the auto rewind, so nothing seeks once audio starts.+ final longPause = pausedFor != null && pausedFor >= _pauseStopTimeout;+ if ((Platform.isAndroid && longPause || _playerBroken) &&+ _player?.processingState != ProcessingState.idle &&+ _currentItemId != null &&+ _api != null) {+ final why = _playerBroken+ ? 'Player failed while paused'+ : 'Long pause (${pausedFor!.inSeconds}s)';+ _playerBroken = false;+ // A position of zero means the player lost its place; the start then+ // comes from the saved progress instead of being forced to the top.+ final startS = position.inMilliseconds / 1000.0;+ debugPrint(+ '[Player] $why - rebuilding the player '+ '${startS > 0 ? "at ${startS.toStringAsFixed(1)}s" : "from the saved position"} '+ 'instead of resuming it',+ );+ _logEvent(PlaybackEventType.play, detail: '$logDetail (rebuilt: $why)');+ unawaited(playItem(+ api: _api!,+ itemId: _currentItemId!,+ title: _currentTitle ?? '',+ author: _currentAuthor ?? '',+ coverUrl: _currentCoverUrl,+ totalDuration: _totalDuration,+ chapters: _chapters,+ startTime: startS,+ forceStartTime: startS > 0,+ episodeId: _currentEpisodeId,+ episodeTitle: _currentEpisodeTitle,+ libraryId: _currentLibraryId,+ fromUi: fromUi,+ ));+ return;+ }// If the player is idle (source was disposed), we need to fully re-initialize// playback instead of just calling play() on an empty player.if (_player?.processingState == ProcessingState.idle &&diff --git a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.javaindex 7c0ffaaf..7a2539c5 100644--- a/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java+++ b/packages/audio_service/android/src/main/java/com/ryanheise/audioservice/AudioService.java@@ -1,5 +1,7 @@package com.ryanheise.audioservice;+import android.app.ActivityManager;+import android.app.ApplicationExitInfo;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;@@ -9,7 +11,10 @@ import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;+import android.media.AudioAttributes;+import android.media.AudioDeviceInfo;import android.media.AudioManager;+import android.media.AudioPlaybackConfiguration;import android.net.Uri;import android.os.Build;import android.os.Bundle;@@ -140,9 +145,155 @@ public class AudioService extends MediaBrowserServiceCompat {snapshot.put("lastPauseCallerAgeMs", lastPauseAt == 0 ? -1 : now - lastPauseAt);snapshot.put("carClientAgeMs", lastCarClientAt == 0 ? -1 : now - lastCarClientAt);snapshot.put("lastKeyPkg", lastMediaKeyPkg);+ snapshot.put("route", describeActiveRoutes());+ snapshot.put("outputs", describeExternalOutputs());+ snapshot.put("musicVolume", describeMusicVolume());+ snapshot.put("lastExit", lastExitInfo);return snapshot;}+ // Why the previous process of this app ended, from the system's own+ // record, so a death with nothing in the log still has a reason.+ private static volatile String lastExitInfo = "unread";++ private void readLastExit() {+ if (Build.VERSION.SDK_INT < 30) {+ lastExitInfo = "n/a";+ return;+ }+ try {+ ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);+ List<ApplicationExitInfo> exits = am.getHistoricalProcessExitReasons(getPackageName(), 0, 2);+ if (exits.isEmpty()) {+ lastExitInfo = "none";+ return;+ }+ StringBuilder sb = new StringBuilder();+ for (ApplicationExitInfo e : exits) {+ if (sb.length() > 0) sb.append(" | ");+ sb.append(exitReasonName(e.getReason()))+ .append(" at ").append(new java.util.Date(e.getTimestamp()))+ .append(" status=").append(e.getStatus())+ .append(" importance=").append(e.getImportance())+ .append(" rss=").append(e.getRss() / 1024).append("MB");+ if (e.getDescription() != null) sb.append(" desc=\"").append(e.getDescription()).append('"');+ }+ lastExitInfo = sb.toString();+ } catch (Exception ex) {+ lastExitInfo = "err:" + ex.getClass().getSimpleName();+ }+ }++ private static String exitReasonName(int reason) {+ switch (reason) {+ case ApplicationExitInfo.REASON_EXIT_SELF: return "exit-self";+ case ApplicationExitInfo.REASON_SIGNALED: return "signaled";+ case ApplicationExitInfo.REASON_LOW_MEMORY: return "low-memory";+ case ApplicationExitInfo.REASON_CRASH: return "crash";+ case ApplicationExitInfo.REASON_CRASH_NATIVE: return "native-crash";+ case ApplicationExitInfo.REASON_ANR: return "anr";+ case ApplicationExitInfo.REASON_INITIALIZATION_FAILURE: return "init-failure";+ case ApplicationExitInfo.REASON_PERMISSION_CHANGE: return "permission-change";+ case ApplicationExitInfo.REASON_EXCESSIVE_RESOURCE_USAGE: return "excessive-resource";+ case ApplicationExitInfo.REASON_USER_REQUESTED: return "user-requested";+ case ApplicationExitInfo.REASON_USER_STOPPED: return "user-stopped";+ case ApplicationExitInfo.REASON_DEPENDENCY_DIED: return "dependency-died";+ case ApplicationExitInfo.REASON_OTHER: return "other";+ case ApplicationExitInfo.REASON_FREEZER: return "freezer";+ case ApplicationExitInfo.REASON_PACKAGE_STATE_CHANGE: return "package-state-change";+ case ApplicationExitInfo.REASON_PACKAGE_UPDATED: return "package-updated";+ default: return "reason" + reason;+ }+ }++ private static String deviceLabel(AudioDeviceInfo d) {+ if (d == null) return "none";+ CharSequence name = d.getProductName();+ return deviceTypeName(d.getType()) + ":" + (name == null ? "?" : name);+ }++ private static String deviceTypeName(int type) {+ switch (type) {+ case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER: return "speaker";+ case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE: return "earpiece";+ case AudioDeviceInfo.TYPE_WIRED_HEADSET: return "wired-headset";+ case AudioDeviceInfo.TYPE_WIRED_HEADPHONES: return "wired-headphones";+ case AudioDeviceInfo.TYPE_USB_HEADSET: return "usb-headset";+ case AudioDeviceInfo.TYPE_USB_DEVICE: return "usb";+ case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP: return "bt-a2dp";+ case AudioDeviceInfo.TYPE_BLUETOOTH_SCO: return "bt-sco";+ case AudioDeviceInfo.TYPE_HEARING_AID: return "hearing-aid";+ case 26: return "ble-headset";+ case 27: return "ble-speaker";+ case 30: return "ble-broadcast";+ case AudioDeviceInfo.TYPE_HDMI: return "hdmi";+ case AudioDeviceInfo.TYPE_REMOTE_SUBMIX: return "remote-submix";+ default: return "type" + type;+ }+ }++ // Where the sound is going right now: every active media player on the+ // device and the output it is routed to. The app's own player is the+ // media one; other apps show up too, which is fine for a diagnostic.+ private static String describeActiveRoutes() {+ final AudioService svc = instance;+ if (svc == null || Build.VERSION.SDK_INT < 31) return "n/a";+ try {+ final AudioManager am = (AudioManager) svc.getSystemService(Context.AUDIO_SERVICE);+ final StringBuilder sb = new StringBuilder();+ for (AudioPlaybackConfiguration c : am.getActivePlaybackConfigurations()) {+ final AudioAttributes attrs = c.getAudioAttributes();+ final int usage = attrs == null ? -1 : attrs.getUsage();+ if (sb.length() > 0) sb.append(',');+ sb.append("usage").append(usage).append("->").append(deviceLabel(c.getAudioDeviceInfo()));+ }+ return sb.length() == 0 ? "none" : sb.toString();+ } catch (Exception e) {+ return "err:" + e.getClass().getSimpleName();+ }+ }++ // Media volume as "level/max" plus "muted" when the stream is muted, so a+ // silent start can be told apart from the phone simply being turned down.+ private static String describeMusicVolume() {+ final AudioService svc = instance;+ if (svc == null) return "n/a";+ try {+ final AudioManager am = (AudioManager) svc.getSystemService(Context.AUDIO_SERVICE);+ String s = am.getStreamVolume(AudioManager.STREAM_MUSIC) + "/"+ + am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);+ if (Build.VERSION.SDK_INT >= 23 && am.isStreamMute(AudioManager.STREAM_MUSIC)) s += " muted";+ return s;+ } catch (Exception e) {+ return "err:" + e.getClass().getSimpleName();+ }+ }++ // Headsets, speakers and cars the phone could send audio to, beyond its+ // own speaker. Bluetooth here while the user hears nothing is the ghost+ // route fingerprint.+ private static String describeExternalOutputs() {+ final AudioService svc = instance;+ if (svc == null || Build.VERSION.SDK_INT < 23) return "n/a";+ try {+ final AudioManager am = (AudioManager) svc.getSystemService(Context.AUDIO_SERVICE);+ final StringBuilder sb = new StringBuilder();+ for (AudioDeviceInfo d : am.getDevices(AudioManager.GET_DEVICES_OUTPUTS)) {+ final int t = d.getType();+ if (t == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER+ || t == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE+ || t == AudioDeviceInfo.TYPE_TELEPHONY+ || t == AudioDeviceInfo.TYPE_REMOTE_SUBMIX+ || t == AudioDeviceInfo.TYPE_FM) continue;+ if (sb.length() > 0) sb.append(',');+ sb.append(deviceLabel(d));+ }+ return sb.length() == 0 ? "none" : sb.toString();+ } catch (Exception e) {+ return "err:" + e.getClass().getSimpleName();+ }+ }+public static void init(ServiceListener listener) {AudioService.listener = listener;}@@ -365,6 +516,7 @@ public class AudioService extends MediaBrowserServiceCompat {public void onCreate() {super.onCreate();instance = this;+ readLastExit();// Absorb patch: if this is a recreated service (a previous instance's// onDestroy nulled the listener), re-register the existing handler so// media buttons keep routing to Dart instead of being dropped indiff --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.javaindex b49ac6fc..fc9d9b04 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@@ -444,11 +444,13 @@ public class AudioPlayer implements MethodCallHandler, Player.Listener, Metadatabreak;case ExoPlaybackException.TYPE_RENDERER:- Log.e(TAG, "TYPE_RENDERER: " + exoError.getRendererException().getMessage());+ sourceCause = describeThrowable(exoError.getRendererException());+ Log.e(TAG, "TYPE_RENDERER: " + sourceCause);break;case ExoPlaybackException.TYPE_UNEXPECTED:- Log.e(TAG, "TYPE_UNEXPECTED: " + exoError.getUnexpectedException().getMessage());+ sourceCause = describeThrowable(exoError.getUnexpectedException());+ Log.e(TAG, "TYPE_UNEXPECTED: " + sourceCause);break;default:@@ -1127,6 +1129,27 @@ public class AudioPlayer implements MethodCallHandler, Player.Listener, MetadatasendError(errorCode, errorMsg, null);}+ // Absorb patch: the class, message and first frames of an ExoPlayer+ // failure, so the Dart log says what broke instead of "runtime error".+ private static String describeThrowable(Throwable t) {+ if (t == null) return "null";+ StringBuilder sb = new StringBuilder(t.getClass().getSimpleName());+ if (t.getMessage() != null) sb.append(": ").append(t.getMessage());+ StackTraceElement[] frames = t.getStackTrace();+ for (int i = 0; i < frames.length && i < 4; i++) {+ String cls = frames[i].getClassName();+ sb.append(" @ ").append(cls.substring(cls.lastIndexOf('.') + 1))+ .append('.').append(frames[i].getMethodName())+ .append(':').append(frames[i].getLineNumber());+ }+ Throwable cause = t.getCause();+ if (cause != null && cause != t) {+ sb.append(" <- ").append(cause.getClass().getSimpleName());+ if (cause.getMessage() != null) sb.append(": ").append(cause.getMessage());+ }+ return sb.toString();+ }+private void sendError(String errorCode, String errorMsg, Object details) {if (prepareResult != null) {prepareResult.error(errorCode, errorMsg, details);