Simple streaming music sample using a foreground service with Media3 ExoPlayer so playback continues when the UI is backgrounded.
- Foreground
ServicewithmediaPlaybacktype - ExoPlayer streaming an internet radio URL
- MediaSession for system integration
- Ongoing notification with Play/Pause action
- Compose UI (Kotlin) with simple button
- Open in Android Studio (Hedgehog+ recommended).
- Let Gradle sync (AGP 8.x, Kotlin 2.0, compileSdk 36).
- Run on a device / emulator with internet access (API 31+ due to minSdk).
- Tap Play to start stream. Press Home: audio continues, notification shows controls.
- Replace test stream URL with your own stream (HLS / MP3) in
MusicForegroundService.ensurePlayer(). - Add audio focus handling and noisy (becoming noisy) broadcast for headphones removal.
- Add proper state flow from Service to UI instead of polling (e.g.,
MutableStateFlow). - Add MediaStyle large icon and metadata updates.
- Support playback controls via Bluetooth / car by expanding MediaSession callbacks.
This sample contains only original code you may adapt freely.