Xamarin MediaPlayer interrupted by Android Doze Mode - android

We have a music app (mp3) developed in Xamarin (Native Android App) using MediaPlayer. There will be list of songs that should be played one after the other. After around 10 minutes or so, the player stops. Once the phone is unlocked, the Player starts playing the next track. Seems the Nougat Doze is somehow intervening the Player. If I manually disable Battery Optimization for my app, then there is no issue.
Is there any fix for this problem rather than having each user to manually change the 'Batter Optimization' option. I wonder how other apps like Spotify plays songs continuously without any setting changes. Please help!

Any process that contains a running foreground service will not be considered for Doze mode. This is what apps like Spotify and Google Play Music use. Consider creating a relevant foreground service - it will be shown in the notification screen while the foreground service is running, so a dummy service will stick out.
Another option is to request the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission and then launch an ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent, which will take the user to the Battery Optimization screen for your app. This is a less-than-stellar user experience but does not require the creation of a foreground service that you may not otherwise need.

Related

Audio Call Stops working when Android App goes in Background and device is on Battery Saver Mode

My question is more like client side.
I am using Janus AudioBridge Room to make audio call.
Android app works fine in foreground and background mode if device is not on Batter saver mode.
But when device is in Battery saver mode. App gets hold while getting into background.
When we gets back to the app. Call rejoins from there. But if it gets long enough to call 'Keepalive' event. Then app will stops or crash.
I handle this by getting this android permission explicitly from user.
"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
Then in battery saver mode. our call does not affects. as our app now can work on background in battery saver mode also.
I don't think this is the best way to do that.
I checked Whatsapp permissions. They are not asking for it.
What Whatsapp or other calling applications are doing to do this in a more proficient way?
Any guide and Clue will be helpful. I appreciate

My player app keeps being killed on some devices

My player app uses a foreground service for playback and yet on some devices playback is killed by the device until the app is excluded from the device Optimized apps list.
It's as if the device has a list of white listed app that won't be killed because this doesn't see to happen for big apps like Spotify, Google Music, ... and yet small player apps get killed.
This doesn't seem to be a code issue. I just want to make sure I'm not imagining this
What do you think about this

How to keep playback active after the screen is turned off

I have an app with audio playback, but on some devices (mainly Huawei, Samsung, Sony) playback is stopped when the screen goes off.
Note: playback does not stop for some other apps like Spotify, Play Music
I found this answer (for Huawei devices) which suggest to ask user to enable "run in background" permission in battery optimization settings (Battery Manager > Protected Apps) but I would like to avoid asking users to enable it.
Is there some API to enable it programmatically for Huawei, Samsung, Sony or other manufacturers?
Is it possible to detect if app do not have such permission?
EDIT: playback runs in foreground service and app holds wakelock
EDIT 2: I've contacted Huawei support related to this issue. I will share more info once resolved.
Use a service with foreground priority the newer Android OS versions have a doze function to save battery.
If you already use the service in foreground probably there is something wrong with the way you use the service in your code (give more details).
Here a tutorial about Playing music in the background that could help you.

Spotify Android SDK with Doze battery optimizations

When I use the Spotify Android SDK to play music and Doze kicks in, the music stops playing because the app loses network connectivity. Is there a way to solve this?
The only way I can think of is ask the user to whitelist the app using a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent. But I read that a lot of developers get their app banned from the play store when doing that.
Running the Spotify instance in a foreground service fixes the problem. See https://developer.android.com/guide/topics/media/mediaplayer.html#foregroundserv

Continue to play audio even if the screen is locked

I am just messing around with an app that streams audio and I wanted to give it a feature similar to Pandora/Google Music/etc where if you press home or lock the screen the audio continues to play in the background.
How exactly can I accomplish this? Is it through a broadcast receiver or a service? If I knew more closely what I was looking for Google would be more helpful.
Thanks!
It's a service. Anytime you want to do something that takes a lot of time like playing music, downloading a lot of data from a server, etc. it should be a service. The basic technique is to always have the service play the music and then have your activity connect to it to show the status and update the tracks, etc.

Categories

Resources