CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.19k stars 375 forks source link

[BUG] MediaElement Android ForegroundServiceStartNotAllowedException when app is not in the foreground and Source is changed #2049

Closed adiamante closed 4 weeks ago

adiamante commented 1 month ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

When automatically changing MediaElement Source when app is not in the foreground, media playback notification disappears and I get the following error:

android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.companyname.mediaelementtest/CommunityToolkit.Maui.Media.Services
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:54)
    at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel(ForegroundServiceStartNotAllowedException.java:50)
    at android.os.Parcel.readParcelableInternal(Parcel.java:4870)
    at android.os.Parcel.readParcelable(Parcel.java:4852)
    at android.os.Parcel.createExceptionOrNull(Parcel.java:3052)
    at android.os.Parcel.createException(Parcel.java:3041)
    at android.os.Parcel.readException(Parcel.java:3024)
    at android.os.Parcel.readException(Parcel.java:2966)
    at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5984)
    at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1931)
    at android.app.ContextImpl.startForegroundService(ContextImpl.java:1906)
    at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:830)
    at crc64ceb75e76f4b66147.MediaManager.n_onPlayerStateChanged(Native Method)
    at crc64ceb75e76f4b66147.MediaManager.onPlayerStateChanged(MediaManager.java:225)
    at com.google.android.exoplayer2.ExoPlayerImpl.lambda$updatePlaybackInfo$20(ExoPlayerImpl.java:2079)
    at com.google.android.exoplayer2.ExoPlayerImpl$$ExternalSyntheticLambda16.invoke(Unknown Source:4)
    at com.google.android.exoplayer2.util.ListenerSet$ListenerHolder.invoke(ListenerSet.java:342)
    at com.google.android.exoplayer2.util.ListenerSet.lambda$queueEvent$0(ListenerSet.java:226)
    at com.google.android.exoplayer2.util.ListenerSet$$ExternalSyntheticLambda0.run(Unknown Source:6)
    at com.google.android.exoplayer2.util.ListenerSet.flushEvents(ListenerSet.java:248)
    at com.google.android.exoplayer2.ExoPlayerImpl.updatePlaybackInfo(ExoPlayerImpl.java:2113)
    at com.google.android.exoplayer2.ExoPlayerImpl.handlePlaybackInfo(ExoPlayerImpl.java:1946)
    at com.google.android.exoplayer2.ExoPlayerImpl.lambda$new$1$com-google-android-exoplayer2-ExoPlayerImpl(ExoPlayerImpl.java:328)
    at com.google.android.exoplayer2.ExoPlayerImpl$$ExternalSyntheticLambda19.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:958)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:205)
    at android.os.Looper.loop(Looper.java:294)
    at android.app.ActivityThread.main(ActivityThread.java:8177)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

  --- End of managed Android.App.ForegroundServiceStartNotAllowedException stack trace ---

Expected Behavior

When automatically changing MediaElement Source when app is not in the foreground, medi playback notification does not disappear.

Steps To Reproduce

  1. Run app on Android (I used a Pixel 5 emulator)
  2. Press play in the media element in the Home Page
  3. Change into another app such as Youtube
  4. Let the music finish and it will change the music source
  5. Media playback notification disappears

Link to public reproduction project repository

https://github.com/adiamante/mediaelementtest

Environment

- .NET MAUI CommunityToolkit: 9.0.2
- OS: Android 14 - API 34
- .NET MAUI: 9.0.2
- .NET MAUI CommunityToolkit MediaElement: 4.0.1

Anything else?

I did some digging around and it's possible that this is an issue with OnStartCommand returning StartCommandResult.Sticky : google issue tracker 307329994

https://github.com/CommunityToolkit/Maui/blob/cc1fb686815587076bbad45a1903f3ec3039315d/src/CommunityToolkit.Maui.MediaElement/Services/MediaControlsService.android.cs#L66

ne0rrmatrix commented 1 month ago

I'm going to test START_NOT_STICKY and see if that fixes the issue. If it does I will create a PR. TY @adiamante for the troubleshooting :)

ne0rrmatrix commented 1 month ago

I have had the community toolkit sample app running in background, with chrome open in the foreground. It has been running for 10 min looping a video from sample app. It has no issues with background playback. Have u tested on a physical device? I am going to test on that now.

adiamante commented 1 month ago

@ne0rrmatrix I'm able to replicate on an Android 13 Motorola Edge. Which emulator did try with?

adiamante commented 1 month ago

Was able to replicate with both Pixel 5 emulator and physical device just now image

ne0rrmatrix commented 1 month ago

I'm running it on a Samsung a34 with android 14. It has been looping through video for 10 min now while I have this open and the phone screen off for most of the time. I am using the sample app from community toolkit to test.

Can u try the toolkit sample and see if I can replicate the behavior u described?

adiamante commented 1 month ago

You're refering to CommunityToolkit.Maui.Sample?

adiamante commented 1 month ago

@ne0rrmatrix I don't think ShouldLoopPlayback changes the Source. Can you try my sample app?

https://github.com/adiamante/mediaelementtest

ne0rrmatrix commented 1 month ago

I figured it out. I will have a fix out later today. The service ends each time you switch source. The service can't restart when not in foreground. If it is backgrounded and service exit's it cannot restart unless in foreground. I tested the fix. It appears to work.

adiamante commented 1 month ago

@ne0rrmatrix Thank you sir 🙏