Swift: Keep playing sounds when the device is locked

后端 未结 4 1027
眼角桃花
眼角桃花 2021-01-15 04:50

I\'m using the following code to play a sound off a URL from the internet:

var audioPlayer = AVPlayer()

...

let audioSessio         


        
4条回答
  •  温柔的废话
    2021-01-15 05:30

    first of all you should add background mode to your plist like this:enter image description here

    Then you should use AVAudioSessionCategoryPlayback session. Like this:

    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    

    AVAudioSessionCategoryAmbient does not work in background.

提交回复
热议问题