添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I have the following code in my app to make all of my AVPlayers play audio in the background as well as foreground. It worked fine in Swift 4.


        do {
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
            NSLog("Playback OK")
            try AVAudioSession.sharedInstance().setActive(true)
            NSLog("Session is Active")
        } catch {
            NSLog("ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: \"\(error)\"")
        }


Then this morning when I converted to Swift 5, I'm now getting an error on line 02 that says "Ambiguous reference to member 'setCategory'". What does this mean and how can I fix it?

try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.mixWithOthers) NSLog("Playback OK") try AVAudioSession.sharedInstance().setActive(true) NSLog("Session is Active") } catch { NSLog("ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: \"\(error)\"") try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.mixWithOthers) NSLog("Playback OK") try AVAudioSession.sharedInstance().setActive(true) NSLog("Session is Active") } catch { NSLog("ERROR: CANNOT PLAY MUSIC IN BACKGROUND. Message from code: \"\(error)\"")
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Developer Forums Participation Agreement .
  • Forums
  • Terms of Use Privacy Policy License Agreements