Background audio is an important feature in many Android applications. Music streaming apps, podcast players, audiobook applications, video platforms, meditation apps, and communication applications often need to continue playing audio when the user leaves the application or locks the device.
For example, a user may start listening to a podcast, open another application to read an email, and expect the podcast to continue playing. Similarly, a music application should not stop playback simply because the user switches to another screen.
Android has supported background audio for many years through services, media sessions, audio focus, and foreground service notifications.
However, Android 17 introduces stricter rules for background audio interactions. These changes affect audio playback, audio focus requests, and volume-control APIs. Applications that do not follow the required lifecycle can experience silent playback failures.
This is an important change because an application may appear to work correctly during development but stop producing audio after the user sends it to the background.
In this article, we will understand the Android 17 background audio restrictions, why they were introduced, how foreground services and while-in-use capabilities affect playback, and how to implement a reliable background audio architecture using AndroidX Media3.
Understanding Background Audio in Android
Before looking at the Android 17 changes, it is useful to understand how background audio normally works.
Consider a music application with the following workflow:
The user opens the application.
The user selects a song.
The application starts audio playback.
The user presses the Home button.
The screen turns off.
The music continues playing.
The user controls playback through the notification or lock screen.
If the application manages playback directly inside an Activity, the implementation can become unreliable.
An Activity is a user-interface component. Its lifecycle changes when the user navigates away, opens another application, or turns off the screen.
The audio player, however, may need to continue working after the Activity is no longer visible.
This is why Android applications generally separate the playback engine from the user interface.
A typical architecture looks like this:
Android Application
|
v
Activity / Fragment
|
v
MediaController
|
v
MediaSession
|
v
MediaSessionService
|
v
ExoPlayer
|
v
Audio Output
Each component has a specific responsibility.
Component | Responsibility |
|---|
Activity | Displays the player interface |
MediaController | Sends playback commands |
MediaSession | Represents the current playback session |
MediaSessionService | Manages playback outside the Activity |
ExoPlayer | Loads, buffers, and plays media |
Android Audio Framework | Manages audio output and system audio behavior |
This separation allows playback to continue when the user leaves the application.
What Changes in Android 17?
Android 17 introduces background audio hardening.
The purpose is to prevent applications from starting or controlling audio unexpectedly when they are not in a valid lifecycle state.
The restrictions apply to several types of audio interactions:
Starting or continuing audio playback.
Requesting audio focus.
Changing stream volume.
Muting or adjusting audio streams.
Changing the device's ringer mode.
The most important point is that these restrictions apply to all applications running on Android 17, not only applications targeting API level 37.
However, applications targeting Android 17 have an additional requirement.
Rules That Apply to All Applications
On Android 17, an application performing the affected background audio interactions must have one of the following:
For example, if a user is watching a video inside a visible Activity, the application can interact with audio while that Activity is visible.
If the user minimizes the application and expects audio to continue, the application should use an appropriate foreground service.
Additional Rules for Applications Targeting API 37
Applications targeting Android 17 must satisfy an additional condition when interacting with audio from the background.
The application must have a foreground service with while-in-use (WIU) capabilities.
There is a specific exception for applications that have the exact alarm permission and are interacting with audio streams configured with AudioAttributes.USAGE_ALARM.
This exception is intended for alarm-related use cases, not ordinary music or podcast playback.
The following table summarizes the requirements.
Application state | Background audio behavior |
|---|
Visible Activity | Audio interaction is allowed |
Background with valid foreground service, app targeting API 36 or lower | Supported |
Background with foreground service and WIU capabilities, app targeting API 37 | Supported |
Background with foreground service but without WIU capabilities, app targeting API 37 | Audio interaction is restricted |
Background without a valid foreground service | Audio interaction is restricted |
Exact alarm permission with USAGE_ALARM audio | Specific exception applies |
A foreground service alone is not sufficient for an application targeting API 37. It must also have the required while-in-use capability unless the alarm exception applies.
What Is a Foreground Service?
A foreground service is a service used for work that the user should be aware of while it is running.
Unlike ordinary background services, a foreground service displays a persistent notification.
For example, when a music application plays a song in the background, Android can display a notification containing:
The notification helps users understand that the application is still performing an ongoing operation.
Android provides different foreground service types for different use cases.
Some examples include:
Foreground service type | Typical use |
|---|
mediaPlayback | Music, podcasts, audiobooks, and video playback |
location | Navigation and location tracking |
camera | Camera-related operations |
microphone | Audio recording |
dataSync | Data synchronization |
connectedDevice | Ongoing interaction with an external device |
For background audio, the correct foreground service type is mediaPlayback.
Do not use dataSync or another unrelated service type simply to keep an application running.
Understanding While-In-Use (WIU) Capabilities
The while-in-use concept is important for understanding Android 17.
Android uses lifecycle and user-interaction rules to determine whether an application is allowed to perform certain operations while it is not visible.
For example, an application may be allowed to start a foreground service while its Activity is visible.
That service receives the appropriate while-in-use capabilities associated with the user-initiated operation.
However, starting a foreground service from the background does not automatically provide the same capabilities.
Consider two scenarios.
Scenario 1: User Starts Playback From the Application
The user opens a music application and presses Play.
The application starts its media playback service while its Activity is visible.
The service continues the playback after the user leaves the application.
This is the expected user-initiated workflow.
Scenario 2: Application Starts Playback Automatically From the Background
Suppose an application starts a service after receiving a BOOT_COMPLETED broadcast.
The service attempts to start music playback without the user opening the application or explicitly requesting playback.
Even if the application has a foreground service, it may not have the required while-in-use capability.
Android 17 can therefore block the audio interaction.
This distinction is important:
A foreground service keeps an eligible operation running, but it does not automatically give every background-started service permission to interact with audio.
What Happens When an Application Violates the Restrictions?
One of the difficult parts of Android 17 background audio hardening is that not every failure produces an obvious exception.
The result depends on the audio API being used.
Audio operation | Result when blocked |
|---|
AudioTrack.write() | Audio is silenced without a normal failure message |
AAudioStream_write | Audio output is suppressed |
OpenSL ES playback | Audio output is suppressed |
Media3 or ExoPlayer playback | Underlying audio output can be suppressed |
AudioManager.requestAudioFocus() | Returns AUDIOFOCUS_REQUEST_FAILED |
AudioManager.setStreamVolume() | Change is ignored |
AudioManager.adjustStreamVolume() | Change is ignored |
AudioManager.setRingerMode() | Change is ignored |
This behavior can be confusing.
For example, an application may successfully create an ExoPlayer instance, prepare a media item, and call play(). The player may appear to be running, but no sound is produced.
The application may not receive an ordinary playback exception.
This is why developers should not rely only on player state when diagnosing background audio problems.
They should also verify the service lifecycle and Android's audio-hardening diagnostics.
Why Did Android Introduce Background Audio Hardening?
Android introduced these restrictions to improve the reliability and predictability of background audio.
There were several problems with older implementations.
Unexpected Audio Playback
An application could start audio without a clear user action.
For example, a service started during device boot might begin playback even though the user had not opened the application.
This creates a poor user experience.
Audio Playback Without a Proper Service
Some applications attempted to continue audio playback without a suitable foreground service.
Depending on system conditions, playback could become interrupted, delayed, or inconsistent.
Playback Detached From the Application Lifecycle
An audio player could continue holding audio focus even after the user believed the playback session had ended.
This could interfere with other applications and create situations where the user had no clear way to stop the audio.
Android 17 encourages developers to connect playback to an explicit user action and a properly managed foreground service.
Recommended Architecture: AndroidX Media3
For most music, podcast, audiobook, and streaming applications, Android recommends using AndroidX Media3 with MediaSessionService.
Media3 provides a standard architecture for background playback.
It handles several responsibilities that developers would otherwise need to implement manually:
Managing the media session.
Maintaining playback independently of the Activity.
Integrating with Android media controls.
Managing the foreground playback notification.
Coordinating playback commands.
Handling audio focus through the player configuration.
This is preferable to building a custom service around AudioTrack unless the application has a specific low-level audio requirement.
Why Use MediaSessionService?
MediaSessionService separates the playback engine from the user interface.
The Activity communicates with the service through MediaController.
The service owns ExoPlayer and MediaSession.
When playback begins, Media3 manages the foreground playback lifecycle and notification.
When playback ends, the service can leave the foreground state according to its lifecycle.
This architecture is designed for applications that need reliable background playback.
Step 1: Add Media3 Dependencies
Add the Media3 libraries to the module-level build.gradle.kts file.
dependencies {
val media3Version = "<current-stable-version>"
implementation(
"androidx.media3:media3-exoplayer:$media3Version"
)
implementation(
"androidx.media3:media3-session:$media3Version"
)
}
Use the same Media3 version for the related Media3 modules.
The ExoPlayer module provides the playback engine, while the session module provides MediaSessionService and MediaController.
Do not mix different Media3 versions in the same application.
Step 2: Declare Foreground Service Permissions
The application must declare the foreground service permissions in AndroidManifest.xml.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<application
...>
<service
android:name=".PlaybackService"
android:foregroundServiceType="mediaPlayback"
android:exported="true">
<intent-filter>
<action
android:name="androidx.media3.session.MediaSessionService" />
</intent-filter>
</service>
</application>
</manifest>
There are three important parts here.
FOREGROUND_SERVICE
This is the general permission required for foreground services.
FOREGROUND_SERVICE_MEDIA_PLAYBACK
This permission identifies the application's intention to use a foreground service for media playback.
It is required for applications targeting Android 14 and higher that use this service type.
foregroundServiceType="mediaPlayback"
This tells Android what kind of foreground work the service performs.
Declaring the correct service type is important because Android applies different rules to different foreground service categories.
Do not declare mediaPlayback for a service that only downloads files or synchronizes application data.
Step 3: Create the PlaybackService
The next step is to create a service that owns the ExoPlayer instance and MediaSession.
Create a Kotlin class named PlaybackService.kt.
import androidx.media3.common.AudioAttributes
import androidx.media3.common.C
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
class PlaybackService : MediaSessionService() {
private var player: ExoPlayer? = null
private var mediaSession: MediaSession? = null
override fun onCreate() {
super.onCreate()
val exoPlayer = ExoPlayer.Builder(this)
.build()
val audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build()
exoPlayer.setAudioAttributes(
audioAttributes,
true
)
player = exoPlayer
mediaSession = MediaSession.Builder(
this,
exoPlayer
).build()
}
override fun onGetSession(
controllerInfo: MediaSession.ControllerInfo
): MediaSession? {
return mediaSession
}
override fun onDestroy() {
mediaSession?.run {
player.release()
release()
}
mediaSession = null
player = null
super.onDestroy()
}
}
Let's understand the important parts.
Creating ExoPlayer
val exoPlayer = ExoPlayer.Builder(this).build()
This creates the actual playback engine.
It handles media loading, buffering, decoding, and playback.
Configuring Audio Attributes
val audioAttributes = AudioAttributes.Builder()
.setUsage(C.USAGE_MEDIA)
.setContentType(C.AUDIO_CONTENT_TYPE_MUSIC)
.build()
Audio attributes tell Android what kind of audio the application is playing.
For a music player, USAGE_MEDIA is appropriate.
Other applications may need different attributes.
For example, an alarm application should use USAGE_ALARM when it is genuinely playing an alarm.
Do not use USAGE_ALARM simply to avoid Android's background audio restrictions.
Handling Audio Focus
exoPlayer.setAudioAttributes(
audioAttributes,
true
)
The second parameter enables ExoPlayer's audio focus handling.
This allows the player to request and respond to audio focus through the configured Media3 implementation.
For example, if another application starts a phone call or a higher-priority audio interaction, the player can respond to the resulting focus changes.
Creating MediaSession
mediaSession = MediaSession.Builder(
this,
exoPlayer
).build()
The MediaSession connects the playback engine to Android's media control system.
It allows compatible external controllers to interact with the application.
These can include:
Returning the Session
override fun onGetSession(
controllerInfo: MediaSession.ControllerInfo
): MediaSession? = mediaSession
This method allows MediaController clients to connect to the service.
For a production application, you can also validate controller identity and restrict commands where necessary.
Step 4: Connect the Activity Using MediaController
The Activity should not create a separate ExoPlayer instance for background playback.
Instead, it should connect to the service through MediaController.
Create a SessionToken and build the controller.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import com.google.common.util.concurrent.ListenableFuture
class PlayerActivity : AppCompatActivity() {
private var controllerFuture:
ListenableFuture<MediaController>? = null
private var mediaController: MediaController? = null
override fun onStart() {
super.onStart()
val sessionToken = SessionToken(
this,
android.content.ComponentName(
this,
PlaybackService::class.java
)
)
controllerFuture = MediaController.Builder(
this,
sessionToken
).buildAsync()
controllerFuture?.addListener(
{
try {
mediaController = controllerFuture?.get()
} catch (exception: Exception) {
exception.printStackTrace()
}
},
mainExecutor
)
}
override fun onStop() {
mediaController = null
controllerFuture?.let {
MediaController.releaseFuture(it)
}
controllerFuture = null
super.onStop()
}
}
The controller is responsible for communicating with the service.
The Activity can be destroyed or recreated without owning the actual playback engine.
The service remains responsible for managing playback.
Step 5: Start Playback After a User Action
The user should explicitly initiate playback while the application is visible.
For example, when the user presses a Play button, send the media item to the controller.
import androidx.media3.common.MediaItem
fun playSong(
mediaController: MediaController,
songUrl: String
) {
val mediaItem = MediaItem.fromUri(songUrl)
mediaController.setMediaItem(mediaItem)
mediaController.prepare()
mediaController.play()
}
The user action is important.
The application should not wait until the user leaves the Activity and then attempt to start a background audio service without the required lifecycle conditions.
The correct sequence is:
User opens the application.
User selects a song.
User presses Play.
The application connects to its playback service.
The service prepares and starts playback.
Media3 manages the playback foreground service and notification.
The user can leave the application while audio continues.
This is the expected model for a background music or podcast application.
Step 6: Handle Pause and Stop Correctly
A production application should distinguish between pausing and permanently ending playback.
Pausing means that the user intends to continue later.
Stopping means that the current playback session has ended.
For example:
fun pausePlayback(
controller: MediaController
) {
controller.pause()
}
To stop playback completely:
fun stopPlayback(
controller: MediaController
) {
controller.stop()
}
The service should not remain in the foreground indefinitely after playback has permanently ended.
Media3 manages its notification and foreground lifecycle as the playback state changes.
Do not manually stop the service every time the user presses Pause without considering the playback-resume experience.
A paused session may still be useful for resuming playback through the notification or another media controller.
For permanent completion, the application should end the session according to its product behavior and release resources when they are no longer needed.
Understanding MediaSession and the Playback Notification
A MediaSession is more than a connection between the Activity and ExoPlayer.
It provides a standard interface through which Android and other applications can understand the current playback state.
For example, when a song is playing, the session can expose:
Media3 uses this information to manage the playback notification.
The notification is important because it gives the user a visible way to control background audio.
A properly implemented playback notification also improves integration with the Android lock screen and external media controllers.
Adding Media Metadata
The application should provide meaningful metadata for every media item.
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
fun createSongItem(
songUrl: String,
title: String,
artist: String
): MediaItem {
val metadata = MediaMetadata.Builder()
.setTitle(title)
.setArtist(artist)
.build()
return MediaItem.Builder()
.setUri(songUrl)
.setMediaMetadata(metadata)
.build()
}
The title and artist can be used by the system notification and compatible media controllers.
Without proper metadata, users may see incomplete or generic playback information.
What If Your Application Does Not Use Media3?
Not every Android application uses ExoPlayer or Media3.
Some applications use:
These implementations are also affected by Android 17 background audio hardening.
If you are using a custom playback engine, you must manage the foreground service lifecycle yourself.
The important requirement is that the service must be active with the correct mediaPlayback type and must have the required while-in-use capability when the application targets API 37.
Starting a Custom Media Playback Foreground Service
A simplified service implementation looks like this:
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.IBinder
class CustomAudioService : Service() {
override fun onStartCommand(
intent: Intent?,
flags: Int,
startId: Int
): Int {
startForeground(
NOTIFICATION_ID,
createPlaybackNotification(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
)
// Initialize or manage the audio engine here.
return START_NOT_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
companion object {
private const val NOTIFICATION_ID = 1001
}
}
The notification must be created using a valid notification channel on Android 8 and later.
The example shows how to promote a service to the mediaPlayback foreground type. It is not a complete audio player.
The service should be started in response to a user action while the Activity is visible.
Do not start this service automatically from a boot receiver just to restore playback.
Also, do not call audio APIs before the service has been correctly promoted and the application satisfies the applicable lifecycle requirements.
Handling Audio Focus Correctly
Audio focus determines how multiple applications share audio output.
For example, suppose a music application is playing a song and the user receives a phone call.
The music application should respond appropriately when the call takes audio focus.
Android provides AudioManager.requestAudioFocus() for this purpose.
However, an application using Media3 should generally allow ExoPlayer to manage audio focus through its audio attributes rather than implementing a second independent focus system.
For custom playback engines, developers must manage focus explicitly.
A simplified example is:
val audioManager =
getSystemService(AudioManager::class.java)
val focusRequest =
AudioFocusRequest.Builder(
AudioManager.AUDIOFOCUS_GAIN
)
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(
AudioAttributes.USAGE_MEDIA
)
.setContentType(
AudioAttributes.CONTENT_TYPE_MUSIC
)
.build()
)
.setOnAudioFocusChangeListener { focusChange ->
when (focusChange) {
AudioManager.AUDIOFOCUS_GAIN -> {
// Resume or increase playback volume.
}
AudioManager.AUDIOFOCUS_LOSS -> {
// Stop playback and release focus.
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT -> {
// Pause playback temporarily.
}
AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK -> {
// Reduce volume temporarily.
}
}
}
.build()
val result = audioManager.requestAudioFocus(
focusRequest
)
if (result == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
// Do not assume audio focus was acquired.
}
This example illustrates custom audio-focus handling.
A production implementation should also abandon audio focus when playback permanently ends.
Most importantly, an application targeting Android 17 cannot solve the background restriction simply by requesting audio focus again.
The application must first satisfy the required Activity or foreground-service lifecycle conditions.
Handling Temporary Audio Interruptions
Audio playback can be interrupted for many reasons.
For example:
A phone call arrives.
Another application temporarily takes audio focus.
Network buffering occurs.
Bluetooth headphones disconnect.
The audio output device changes.
The user pauses playback temporarily.
These situations do not always mean the playback session has ended.
Android's background audio guidance recommends keeping the mediaPlayback foreground service active during transient failures lasting less than 10 minutes.
For example, if a podcast is buffering because of a temporary network problem, the application should not immediately destroy its playback service.
However, when playback permanently ends, the application should stop the foreground service and end the media session.
Examples of permanent playback completion include:
The user explicitly stops playback.
The media item completes and there is no automatic next item.
The application receives a permanent audio focus loss.
An unrecoverable playback error occurs.
The user ends the playback session through the media controls.
If the user later presses Play again, the application should begin a new user-initiated playback operation and establish the appropriate foreground service lifecycle.
Handling Alarm Applications Separately
Alarm applications have a different audio requirement from music applications.
A clock application may need to play an alarm when the screen is off and the application is not visible.
Android 17 provides a specific exception for applications that have been granted exact alarm permission and are interacting with audio streams configured with USAGE_ALARM.
This exception should be used only for genuine alarm functionality.
For example, an alarm application can configure its audio attributes as follows:
val alarmAttributes =
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(
AudioAttributes.CONTENT_TYPE_SONIFICATION
)
.build()
The application must also meet the exact alarm permission requirements that apply to its target SDK and use case.
Do not use USAGE_ALARM for music, podcasts, or ordinary video playback.
Android uses audio usage attributes to understand the purpose of an audio stream. Incorrectly labeling media as an alarm can produce inappropriate system behavior.
Applications Using Picture-in-Picture
Some video applications continue playing audio while displaying video in Picture-in-Picture mode.
Picture-in-Picture is still a visible user experience.
Android's background audio guidance explicitly treats a visible Activity, including PiP, as a valid visible state for these restrictions.
For a video application that only plays while the user can see the PiP window, the visible Activity can satisfy the visibility requirement.
However, if the application also supports continuing audio after the PiP window is closed or the Activity is no longer visible, it should use the appropriate mediaPlayback foreground service architecture.
This distinction is important for applications that provide both video playback and audio-only background playback.
Testing Background Audio Hardening on Android 17
Testing is particularly important because some failures are silent.
Android provides an ADB command to enable or disable audio hardening during development.
Connect an Android 17 device or emulator and execute the following command:
adb shell cmd audio set-enable-hardening enable
This enables background audio hardening for testing.
To disable the restrictions:
adb shell cmd audio set-enable-hardening disable
There is also a testing mode that makes failures more visible:
adb shell cmd audio set-enable-hardening throw
The throw mode is useful during development because it can turn otherwise silent failures into more obvious errors.
For example, volume and audio focus operations may throw an IllegalStateException, while some playback paths return an error or crash.
Use this mode in a controlled test environment rather than assuming that production devices will expose the same errors.
Understanding the Three Testing Modes
Command option | Purpose |
|---|
enable | Enforces audio hardening restrictions |
disable | Disables the restrictions for testing |
throw | Enforces restrictions and makes failures more visible |
The testing command applies the stricter testing behavior across applications, including the WIU checks.
Therefore, it is useful for finding problems before targeting API 37.
Diagnosing Failures With Logcat and dumpsys
If an application experiences silent audio failures, inspect the Android audio logs.
Use:
adb logcat | grep AudioHardening
You can also inspect the audio system:
adb shell dumpsys audio
Android logs diagnostic messages with the AudioHardening prefix.
Two useful diagnostic levels are:
Log level | Meaning |
|---|
level: partial | The application does not have a valid foreground service for the audio interaction |
level: full | A foreground service is running, but it does not have the required while-in-use capability |
For example, if an application is running a mediaPlayback service but it was started from the background without WIU capability, the diagnostic information can help identify that specific problem.
This is much more useful than repeatedly changing the ExoPlayer configuration without checking the service lifecycle.
Common Mistakes When Implementing Background Audio
Starting Playback From a Background Receiver
Starting playback automatically from BOOT_COMPLETED or another background event is not the expected user-initiated model.
If the user wants to resume playback, provide an explicit action through the application UI or a supported media control.
Using a Foreground Service Without the Correct Type
A generic foreground service does not automatically qualify as a media playback service.
Declare mediaPlayback in the service manifest and include the corresponding permission.
Starting the Service Too Late
A common mistake is waiting until the Activity becomes invisible before starting the playback service.
For applications targeting Android 17, this may result in a foreground service without the required WIU capability.
Start the playback service as part of the user-initiated playback workflow while the Activity is visible.
Assuming ExoPlayer Handles Every Lifecycle Requirement
ExoPlayer manages playback and can handle audio focus, but it does not eliminate the need for a correct foreground service architecture.
Use MediaSessionService for background playback and configure the manifest correctly.
Ignoring Audio Focus Failures
Do not assume that an audio focus request always succeeds.
Check the return value when using AudioManager directly.
When using Media3, monitor player state and playback errors as part of normal diagnostics.
Keeping the Service Active After Playback Ends
A foreground service should not continue indefinitely after the user has finished playback.
Stop the service and release the media session when the playback interaction has permanently ended.
Treating Every Pause as a Permanent Stop
A user may pause a song and expect to resume it later from the notification.
Do not destroy the playback session unnecessarily.
Separate temporary pause behavior from the end of a playback session.
Using the Alarm Exception for Normal Media
USAGE_ALARM and exact alarm permission are intended for alarm-related functionality.
Do not use them as a workaround for music or podcast playback restrictions.
Recommended Background Audio Architecture
For most production applications, the following architecture is a good starting point:
PlayerActivity
|
| User presses Play
v
MediaController
|
v
MediaSessionService
|
+---- MediaSession
|
+---- ExoPlayer
|
+---- Audio Focus
|
+---- Playback Notification
|
v
Android Audio Framework
The Activity handles user interaction, while the service owns playback.
This design has several advantages:
Playback can continue after the Activity is destroyed.
Android can display standard media controls.
Audio focus is handled through the player configuration.
The foreground service lifecycle is connected to playback.
External media controllers can interact with the session.
The application has a clear place to manage playback completion and cleanup.
Android 17 Background Audio Migration Checklist
Before releasing an application targeting API 37, review the following items.
Area | What to verify |
|---|
Playback architecture | Playback is managed outside the Activity |
Foreground service | Uses the mediaPlayback type |
Manifest | Includes required foreground service permissions |
User interaction | Playback starts from an appropriate user action |
WIU capability | Background playback service has the required capability |
Audio focus | Requests and interruptions are handled correctly |
Notifications | Playback notification provides useful controls |
Temporary failures | Service remains active for short transient failures |
Permanent stop | Service and media session are ended appropriately |
Alarm handling | USAGE_ALARM is used only for genuine alarms |
Testing | Audio hardening has been tested with ADB |
Diagnostics | AudioHardening logs have been reviewed |
Conclusion
Android 17 introduces stricter background audio rules to make audio interactions more predictable and ensure that playback is connected to an intentional user action.
The most important requirement is that an application performing background audio interactions must have a valid foreground service. Applications targeting API level 37 or higher must also ensure that the service has while-in-use capabilities, except for the specific exact-alarm and USAGE_ALARM case.
For music, podcast, audiobook, and streaming applications, AndroidX Media3 with MediaSessionService is the recommended architecture. It separates playback from the Activity, manages the media session, and integrates with Android's foreground playback notification.
If your application uses a custom audio engine, review its foreground service lifecycle and audio focus implementation carefully.
Finally, test with Android's audio-hardening ADB commands and inspect AudioHardening logs. Since some failures are silent, checking only whether ExoPlayer reports that it is playing is not enough.
A reliable background audio implementation should make it clear when playback starts, keep the service active while playback is intended to continue, and release the session when the user has finished.
Join the conversation! Your thoughts help the community grow.