Literally 2 lines of code required:
- Send AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION intent with the actual session id when playback starts
- Send AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION intent when playback ends
Additional hints:
-
don't change session id between played tracks, playlists, etc. Keep single session id per playback session (per playing service lifetime)
- this reduces possible artefacts on session start, as effects require some time to setup and apply
- also this approach reduces resources usage as no tear-down/re-setup of effects is required
-
don't use multiple session ids even if you use multiple MediaPlayers/AudioTracks (for example, for the crossfade)
- this also reduces resources usage as there is need for multiple effects then
To implement the hints above, get audio session id once from the first MediaPlayer/AudioTrack and reuse it for the next created MediaPlayers/AudioTracks.