I'm encountering an issue with my Android app related to Android's DOZE mode optimization, particularly concerning maintaining internet connectivity for audio streaming when the device screen turns off.
Here's the scenario with my app:
- The user initiates playback of an audio track.
- The device screen turns off.
- After approximately 3 minutes, the app loses internet connection, causing the audio streaming to stop.
- When the user re-enables the screen, audio gets played after a delay.
This issue persists on devices running Android 13 (Custom ROM on Xiaomi) and a Pixel 6a with Android 14. Interestingly, this problem doesn't occur when the device is connected to a charger. It doesn't matter whether phone is connected to LTE or Wi-Fi.
My question is: Is there a way to execute Dart code (in my case, an audio download job) within the audio_service Isolate to circumvent the impact of Android's DOZE mode optimization on internet connectivity?
According to Android documentation, code associated with foreground notifications remains unaffected by DOZE mode.
I've kept my eye on the IsolatedAudioHandler:
// Wrap audio handler in IsolatedAudioHandler:
_audioHandler = await AudioService.init(
builder: () => IsolatedAudioHandler(
MyAudioHandler(),
portName: 'my_audio_handler',
),
);
// From another Isolate, obtain a proxy reference:
_proxyAudioHandler = await IsolatedAudioHandler.lookup(
portName: 'my_audio_handler',
);
but unfortunately, it seems like IsolatedAudioHandler doesn't allow code to run inside the MyAudioHandler's Isolate.