There is no alternative to attach an AudioEffect to the global output. What you should do instead is register a broadcast receiver that receives all audio sessions, so you can apply audio effects to that. An example can be found here. The intent containing the session ID is obtained in this BroadcastReceiver. Remember that this only works if you registered the receiver in the manifest.
Alternatively you could register a receiver programmatically like this in your service onCreate():
IntentFilter()
.apply { addAction(AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION) }
.apply { addAction(AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION) }
.run { registerReceiver(mAudioSessionReceiver, this) } `
where mAudioSessionReceiver looks something like this:
private val mAudioSessionReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null || context == null) {
return
}
val sessionStates = arrayOf(
AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION,
AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION
)
if (sessionStates.contains(intent.action)) {
val service = Intent(context, WaveletService::class.java)
val sessionID = intent.getIntExtra(AudioEffect.EXTRA_AUDIO_SESSION, AudioEffect.ERROR)
service
.apply { action = intent.action }
.apply { putExtra(AudioEffect.EXTRA_AUDIO_SESSION, sessionID) }
context.startService(service)
}
}
}`
Then, you can obtain the intent in onStartCommand:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null)
return START_STICKY
val sessionID = intent.getIntExtra(AudioEffect.EXTRA_AUDIO_SESSION, AudioEffect.ERROR)
when (intent.action) {
AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION -> {
// create new instance of the AudioEffect using the sessionID
}
AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION -> {
// Release instance of the AudioEffect connected to this sessionID
}
}
return START_REDELIVER_INTENT
}`
Lastly, don't forget to unregister the receiver in onDestroy():
unregisterReceiver(mAudioSessionReceiver)`