You need a Foreground service to keep the WiFi / Network connection alive and don't fall into Doze mode.
Try this APK and test it with / without Foreground service:
- don't charge the device while testing
- start the app, click play and press the power button to put the device in the background (screen off).
- without Foreground service the sound will stop after ~ 4-6 min
- with Foreground service the sound (looped) will play forever ...

Note:
A foreground service is not required for every device, but on some devices (such as Samsung Galaxy Note 8 or Galaxy S8, Android 9, ...) WiFi, clock etc. are shut down by Doze.
