-
-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(android): avoid service crashes #1974
Conversation
Based on @kyunkakata’s work at doublesymmetry#1666 (comment)
ec55c89
to
8eae14d
Compare
android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt
Outdated
Show resolved
Hide resolved
07095e3
to
209fb15
Compare
209fb15
to
ef8f162
Compare
android/src/main/java/com/doublesymmetry/trackplayer/module/MusicModule.kt
Outdated
Show resolved
Hide resolved
8054f2a
to
6f51889
Compare
@kyunkakata did you figure out a way to consistently cause these crashes to happen? I would like to test to make sure everything is fixed now.. |
The crash "Context.startForegroundService() did not then call Service.startForeground() within 5s" is easy to reproduce. Just calling setupPlayer without invoking add function. An ANR or a crash should happen. |
Just to make sure: did the |
So I added back the ANR fix, since it is necessary when you don't want to start the exoplayer notification within 5 seconds of starting MusicService. I also reject the @kyunkakata any feedback is welcome! |
android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever workaround! There are some typescript issues but this is looking good to go.
Thanks @puckey and @kyunkakata
2ac70da
to
b6a4b9e
Compare
Fixed the ts issues, so should be good to go |
- reject promise when calling setupPlayer in the background - in demo app retry setupPlayer while it fails due to being in the background - Add back and extract ANR fix - Fix R import - Remove unnecessary foreground check in NotificationState.POSTED - use DefaultLifecycleObserver over deprecated LifecycleObserver
b6a4b9e
to
2efefc6
Compare
Nice Work! Thank you everyone for the fix, but we still experience the issue, due to this line
maybe this fix should be included |
if (AppForegroundTracker.foregrounded && it.notificationId == MUSIC_NOTIFICATION_ID) { | ||
startForeground(it.notificationId, it.notification) | ||
} | ||
startForeground(it.notificationId, it.notification) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MortadhaFadhlaoui Could you open a new pr for this with some explanation on your changes? Thanks!
I'm not entirely sure the service should be foregrounded immediately after the notification is posted. Could this, together with START_STICKY which restarts the service if killed be the reason for the mAllowForeground error? In the Pocket Casts repo they only use setForeground when they start playing sound. https://github.com/Automattic/pocket-casts-android/blob/ee8da0c095560ef64a82d3a31464491b8d713104/modules/services/repositories/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/playback/PlaybackService.kt#L250 |
* fix(android): avoid service crashes Based on @kyunkakata’s work at doublesymmetry#1666 (comment) * fix(android): reject setupPlayer if backgrounded * chore(android): Remove superfluous fix as per doublesymmetry#1974 (comment) * fix(android): When backgrounded, call setupPlayer when it comes to foreground. * chore(android): remove more remnants of superfluous fix * chore(android): remove unused imports * chore(android): inline MusicModule#eventHandler since it is never mutated.. * chore(android): simplify readableArrayToTrackList * chore(android): remove unnecessary try catch from setupPlayer * chore(android): refactor MusicModule#getConstants() * fix(android): add back anr fix and reject promise when in bg - reject promise when calling setupPlayer in the background - in demo app retry setupPlayer while it fails due to being in the background - Add back and extract ANR fix - Fix R import - Remove unnecessary foreground check in NotificationState.POSTED - use DefaultLifecycleObserver over deprecated LifecycleObserver * chore(docs): background error thrown in setupPlayer
* fix(android): avoid service crashes Based on @kyunkakata’s work at doublesymmetry#1666 (comment) * fix(android): reject setupPlayer if backgrounded * chore(android): Remove superfluous fix as per doublesymmetry#1974 (comment) * fix(android): When backgrounded, call setupPlayer when it comes to foreground. * chore(android): remove more remnants of superfluous fix * chore(android): remove unused imports * chore(android): inline MusicModule#eventHandler since it is never mutated.. * chore(android): simplify readableArrayToTrackList * chore(android): remove unnecessary try catch from setupPlayer * chore(android): refactor MusicModule#getConstants() * fix(android): add back anr fix and reject promise when in bg - reject promise when calling setupPlayer in the background - in demo app retry setupPlayer while it fails due to being in the background - Add back and extract ANR fix - Fix R import - Remove unnecessary foreground check in NotificationState.POSTED - use DefaultLifecycleObserver over deprecated LifecycleObserver * chore(docs): background error thrown in setupPlayer
@martinmidtsund care to submit a PR or test your theory on a fork? |
@jspizziri Yes, I'll be testing this and can submit a PR. Can't set a timeline though, don't know when I will get the time. |
I wonder though – isn't there a big chance the app will be in the background already by the time the sound starts playing? i.e. the users changes the track and moves to another app directly |
@puckey Yes, but don't you think in those cases the mAllowForeground is true, not false? It should be allowed to start a foreground service from the foreground, and from a notification, if I'm not mistaken. This has to be tested of course. |
@martinmidtsund shall we regroup on this with a fresh issue containing stack traces as experienced using the latest rc release? Would you mind opening one? |
@puckey Sorry, we are not using v4 yet, so we don't have any stack traces as of now. I could create a new issue with the stack traces we do have, but they are from v3.2.0. Not sure if that helps, but I would guess it's still the same problem in the new RCs? |
* fix(android): avoid service crashes Based on @kyunkakata’s work at doublesymmetry/react-native-track-player#1666 (comment) * fix(android): reject setupPlayer if backgrounded * chore(android): Remove superfluous fix as per doublesymmetry/react-native-track-player#1974 (comment) * fix(android): When backgrounded, call setupPlayer when it comes to foreground. * chore(android): remove more remnants of superfluous fix * chore(android): remove unused imports * chore(android): inline MusicModule#eventHandler since it is never mutated.. * chore(android): simplify readableArrayToTrackList * chore(android): remove unnecessary try catch from setupPlayer * chore(android): refactor MusicModule#getConstants() * fix(android): add back anr fix and reject promise when in bg - reject promise when calling setupPlayer in the background - in demo app retry setupPlayer while it fails due to being in the background - Add back and extract ANR fix - Fix R import - Remove unnecessary foreground check in NotificationState.POSTED - use DefaultLifecycleObserver over deprecated LifecycleObserver * chore(docs): background error thrown in setupPlayer
Based on @kyunkakata’s work at #1666 (comment)