Skip to content
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

Crash on Android Oreo and higher #5

Open
ghost opened this issue Aug 27, 2018 · 6 comments
Open

Crash on Android Oreo and higher #5

ghost opened this issue Aug 27, 2018 · 6 comments

Comments

@ghost
Copy link

ghost commented Aug 27, 2018

  1. Open Application
  2. Press "Play"
  3. Press "Pause" in notification
  4. Switch to the next song in the notification

ANR appears after five seconds

Reason: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{36d7342 u0 ru.vinyarsky.androidaudioexample/.service.PlayerService}

@ghost
Copy link
Author

ghost commented Oct 15, 2018

Any idea how to fix this?

@SergeyVinyar
Copy link
Owner

Sorry for ignoring the bug. I'm a bit out of time.

This is connected to new background threads limitations presented in Oreo:
https://developer.android.com/about/versions/oreo/android-8.0-changes#back-all

It looks like you just need to invoke
service.startForeground(NOTIFICATION_ID, getNotification(playbackState))
inside service's onCreate method after line
notificationManager.createNotificationChannel(notificationChannel);

And you might also need to remove calling stopForeground when pausing. But i'm not sure.

Hope it helps.

@ghost
Copy link
Author

ghost commented Oct 15, 2018

Can you commit fix and update api version to 28? It will be good

@isabsent
Copy link
Contributor

isabsent commented Sep 24, 2019

Works fine on Androids starting with O and higher after migration to AndroidX and android.permission.FOREGROUND_SERVICE permission in Manifest. You can check it in my fork.

@SergeyVinyar
Copy link
Owner

Unfortunately I've managed to reproduce the bug again. You have to wait for some time after switching the track via notification.
Look at MediaButtonReceiver#onReceive, Each time it gets a broadcast message, PlayerService is started with context#startForegroundService. It happens even when clicking on Next button.
It seems we need to keep the service foreground all the time. Not just during the playback.

@isabsent
Copy link
Contributor

isabsent commented Sep 27, 2019

Sorry, didn't understand this bug correctly from the first reading. I need to play a single track only (a voice message playback in a messenger) and didn't see it.

By the way, I can't disable Next and Previous controls in the notification. Commenting corresponding builder.addActions in getNotification and PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS in the stateBuilder leads to the Graphical interface of the system crashes... dialog while the application continues to work on Android API 24 when the playback button (not in the notification) is pressed with the stack trace:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.get(ArrayList.java:411)
    at android.app.Notification$MediaStyle.makeMediaContentView(Notification.java:5276)
    at android.app.Notification$MediaStyle.makeContentView(Notification.java:5197)
    at android.app.Notification$Builder.createContentView(Notification.java:3587)
    at com.android.systemui.statusbar.NotificationData$Entry.cacheContentViews(NotificationData.java:141)
    at com.android.systemui.statusbar.BaseStatusBar.inflateViews(BaseStatusBar.java:1730)
    at com.android.systemui.statusbar.BaseStatusBar.createNotificationViews(BaseStatusBar.java:2404)
    at com.android.systemui.statusbar.phone.PhoneStatusBar.addNotification(PhoneStatusBar.java:2023)
    at com.android.systemui.statusbar.BaseStatusBar$7$2.run(BaseStatusBar.java:727)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

Does MediaSessionCompat requires all 5 controls should be presented in notification UI?

P.S.: Solved by setting setShowActionsInCompactView(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants