Allow users background music in swift 2.0

后端 未结 1 1519
谎友^
谎友^ 2021-02-14 20:23

I am looking for some code to allow the user to play music from their phone while still using my app. Previously before swift 2.0 i would put this in the app delegate and it wou

相关标签:
1条回答
  • 2021-02-14 21:07

    The following would be the syntax for Swift 2 calling setCategory and setActive on AVSession:

    do
    {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch let error as NSError
    {
        print(error)
    }
    

    OR

    do
    {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch let error as NSError
    {
        print(error)
    }
    
    0 讨论(0)
提交回复
热议问题