I built an app that starts a foreground service with a MediaPlayer instance, when it receives android.intent.action.BOOT_COMPLETE
When my phone updated to API 37 (Android 17), the media player stopped playing sound.
I know it was still working, as I have a notification show itself, whenever the foreground service starts.
That notification never went away.
Additionally, my audio focus requests are also being denied.
Here is the code showing the android manifest (where I ask to listen for BOOT_COMPLETE), the BroadcastReciever (that fires when BOOT_COMPLETE shows up), and the MediaService (the foreground service that BroadcastReciever runs, upon receiving BOOT_COMPLETE)
The code for the notification is not shown in the code I provided, but I can assure you it is physically inside the foreground service file.
android manifest:
<receiver
android:name=".receiver.ChimeReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
BroadcastReceiver
class ChimeReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
val intent = Intent(context, MediaService::class.java)
context.startForegroundService(intent)
} else {
//do other stuff...
}
}
}
MediaService:
As this issue includes being denied when asking for audio focus, I have included the code for that as well.
private fun requestAudioFocus(): Boolean {
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
val audioFocusChangeListener = AudioManager.OnAudioFocusChangeListener {focusChange ->
null
}
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
.setAudioAttributes(attributes)
.setAcceptsDelayedFocusGain(false)
.setWillPauseWhenDucked(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.build()
audioManager.requestAudioFocus(audioFocusRequest)
private fun playsound() {
requestAudioFocus()
private val audioAttributes: AudioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
mediaPlayer = MediaPlayer.create(this, R.raw.rxample, audioAttributes, audioManager.generateAudioSessionId())
mediaPlayer?.start()
}
As I said before, for some reason, my foreground service is starting, but nothing is being played.
these API 37 developer notes talk specifically about android 17's new While-In-Use rules, which have to do with playing media in the background.
Basically, no audio playback is tolerated unless it is initiated by a user action.
However,there is at least one exception:
A. The app has been granted the exact alarm permission,
AND
B. It is making changes to ( / playing) audio streams that have the USAGE_ALARM attribute.
Both have to be true, or android will silence mediaPlayer, without error or warning.
Here is the fix:
For requesting audio focus:
private fun requestAudioFocus(): Boolean {
val attributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
val audioFocusChangeListener = AudioManager.OnAudioFocusChangeListener {focusChange ->
null
}
audioFocusRequest = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)
.setAudioAttributes(attributes)
.setAcceptsDelayedFocusGain(false)
.setWillPauseWhenDucked(true)
.setOnAudioFocusChangeListener(audioFocusChangeListener)
.build()
audioManager.requestAudioFocus(audioFocusRequest)
change requestAudioFocus() > AudioAttributes.Builder() > .setUsage(AudioAttributes.USAGE_MEDIA) to AudioAttributes.USAGE_ALARM
And for playing media in the background:
private fun playsound() {
requestAudioFocus()
private val audioAttributes: AudioAttributes = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
mediaPlayer = MediaPlayer.create(this, R.raw.rxample, audioAttributes, audioManager.generateAudioSessionId())
mediaPlayer?.start()
}
change playsound() > AudioAttributes.Builder() > .setUsage(AudioAttributes.USAGE_MEDIA) to USAGE_ALARM
And of course, somewhere in my app (not listed here), I have an exact alarm scheduling the service as well. That means my app has the exact alarm permission, which is the next requirement.
This was a perfect fix for me, but it does mean having to deal with the fade-in effect that comes with USAGE_ALARM
You'll see what I mean.