Change the Volume for ApplicationMusicPlayer to hear the voice synthesizer?

My app uses the voice synthesizer, and I'm trying to integrate apple music into my app using MusicKit to play a playlist during a workout. I want to lower the volume of the music while the synthesizer is speaking so the user can hear it.

When I play music locally using AVAudioPlayer this is possible. I'm using the ApplicationMusicPlayer to play the tracks of the playlist. I don't see a way to lower the volume ApplicationMusicPlayer programmatically.

Is there a way to programmatically change the volume of the ApplicationMusicPlayer? Or is there a better way to accomplish what I'm trying to do?

thanks

The answer was to set the audio session to duckmode. Before using the speech synthesizer I set the session to duck mode and then I set it back to mixed mode after. This causes music played through musickit to lower in volume.

It doesn't work with AVAudioPlayer, so for music I play through AVAudioPlayer I still have to lower the volume while the synthesizer is playing.

It also doesn't work well when musickit is playing, the synthezier is playing, and there's a sound effect playing through AvAudioPlayer. There's no way for the session to be configured correctly.

func setDuckMode() {

    do {
        try AVAudioSession.sharedInstance().setActive(false)
        
        try AVAudioSession.sharedInstance().setCategory(
            .playback,
            mode: .default,
            options: [.duckOthers])
        
        try AVAudioSession.sharedInstance().setActive(true)
       
    } catch {
        print( "Could not init sound duck. \(error)")
    }

}
Change the Volume for ApplicationMusicPlayer to hear the voice synthesizer?
 
 
Q