XamarinMediaManager icon indicating copy to clipboard operation
XamarinMediaManager copied to clipboard

MAUI Android audio stops after running in the background for about 15 minutes

Open copang opened this issue 2 years ago • 10 comments

I'm using the latest version (1.2.2) for my MAUI app (.net 7) and I'm having problems running it on Android. After the app goes into the background for about 15 minutes the audio stops and when I go back to the app it restarts instead of resuming the current state. I noticed that on the notification manager there is already a player, so I think it has been registered as a service to be able to run in the background. I tried to build in release mode but the result is the same. And of course I have turned off the battery saving modes on the devices already. The devices I am testing are Xiaomi, One plus (Android 12 & 13). Also, I accidentally discovered that if the phone is connected to the charging cable (which is charging) the audio can play for hours in the background.

  • Platform: Android (Android 12 & 13)
  • MAUI (.net 7)
  • Plugin.MediaManager 1.2.2

copang avatar May 24 '23 04:05 copang

@ArchangelWTF @janwiebe-jump can you help me?

copang avatar May 24 '23 04:05 copang

same here. I noticed the app is crashing/closing after 10-15 mins when the app is in the background/foreground.

arahmancsd avatar Aug 01 '23 15:08 arahmancsd

Same issue here. It woks fine when the phone is charging, but otherwise after 2-3 minutes the audio stops. I'm trying to play an stream.

Platform: Android (Android 13) MAUI (.net 7) Plugin.MediaManager 1.2.2

efhayati avatar Aug 20 '23 22:08 efhayati

Maybe you need to aquire a wifi lock to support streaming content.

https://github.com/jamesmontemagno/AndroidStreamingAudio/blob/master/Part%202%20-%20Lock%20Screen/Services/StreamingBackgroundService.cs

sandreas avatar Sep 01 '23 17:09 sandreas

This is happening because the service is never set to Foreground which is due to the initialization of the PlayerNotificationManager is happening incorrectly. The PlayerNotificationManager is being built on line 118. but one of it's parameters isn't is null when it's invoked: https://github.com/Baseflow/XamarinMediaManager/blob/develop/MediaManager/Platforms/Android/MediaSession/MediaBrowserService.cs#L118

Since the NotificationListener is properly constructed on the next line, switching those two lines will resolve the problem.

szurgot avatar Oct 04 '23 21:10 szurgot

Android devices has battery optimization settings for the apps. Could you try to get this permission and see that's work for you. if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { String? packageName = Platform.CurrentActivity?.PackageName; Android.Content.Intent intent = new Android.Content.Intent(); intent.SetAction(Android.Provider.Settings.ActionRequestIgnoreBatteryOptimizations); intent.SetData(Android.Net.Uri.Parse("package:" + packageName)); Platform.CurrentActivity?.StartActivity(intent); }

Also add this to manifest <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

ikeremozcan avatar Apr 04 '24 11:04 ikeremozcan

For anyone looking at this, @szurgot is right switching those two lines solves the problem. Additionally, you need to add the following permission in your manifest file.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"/>

I use MediaManager only, so I forked the project and upgraded it to .net 8. The app crashes without permission. So switch the lines and add the permission in your manifest file and enjoy the library.

arahmancsd avatar Jul 23 '24 14:07 arahmancsd

Can also confirm the above fixes the issue.

BenDTU avatar Jul 23 '24 14:07 BenDTU

@arahmancsd

so I forked the project and upgraded it to .net 8

Is it this one? I see no .net8 specific changes and would be very interested.

sandreas avatar Jul 23 '24 15:07 sandreas

@arahmancsd

so I forked the project and upgraded it to .net 8

Is it this one? I see no .net8 specific changes and would be very interested.

I didn't commit my code as I only upgraded the MediaManager project which I needed it. I have just upgraded all the repos and builds as expected.

arahmancsd avatar Jul 23 '24 18:07 arahmancsd