iosswiftuikitavplayeravaudiosession

stop background music of other app only when the video is unmuted in my app Swift UIKit


applicationDidBecomeActive

 do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .duckOthers)
    } catch let error {
        print(error.localizedDescription)
    }

The audio by other app will not stop when the video is playing in my app, just reduce the volume, this is what I want

self.player.isMuted = true

video will play in mute mode, I want to achieve that When I unmute my video, so other apps audio should stop Like in Facebook

I try to set when mute / unmute button tapped:

do {
        try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: .interruptSpokenAudioAndMixWithOthers)
    } catch let error {
        print(error.localizedDescription)
    }

pause spoken audio content from other sessions when app plays its audio but It does not work even I set

try AVAudioSession.sharedInstance().setActive(true)

Solution

  • The default behavior is to stop other apps' audio. If you want that default behavior, don't pass .duckOthers or .interruptSpokenAudioAndMixWithOthers. Both of these say you don't want to stop other audio. So just use:

    try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)