iOS: How do I detect if music is playing in any background music app?

后端 未结 4 749
南笙
南笙 2020-12-15 21:32

I currently have my game correctly handling disabling its own BGM when music is playing in the built-in iPod app, but it does not detect when an app such as Pandora is playi

4条回答
  •  情深已故
    2020-12-15 22:09

    You may want to do something like this.....

    1. Create A class to handle your audio settings say... "AudioManager"

    2. Poll the Boolean "isOtherAudioPlaying"... maybe assign it to your own Boolean value.

    import Foundation
    import AVFoundation
    
    class AudioManager {
        static let successBingSoundID: SystemSoundID = 
        static func playSystemSoundIfBackgroundSoundIsOff() {
            guard !AVAudioSession.sharedInstance().isOtherAudioPlaying else {return}
            AudioServicesPlaySystemSoundWithCompletion(successBingSoundID, nil)
        }
    }
    

    usage:

    AudioManager.playSystemSoundIfBackgroundSoundIsOff()
    

提交回复
热议问题