I\'m using the following code to play a sound off a URL from the internet:
var audioPlayer = AVPlayer()
...
let audioSessio
Open your app info.plist as Source code (to edit in text mode) and add the following just after dict:
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
swift 2+ solution:
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
return print("error")
}
first of all you should add background mode to your plist like this:
Then you should use AVAudioSessionCategoryPlayback session. Like this:
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSessionCategoryAmbient does not work in background.
You use wrong category, for AVAudioSessionCategoryAmbient
it's normal behavior "Your audio is silenced by screen locking and by the Silent switch".
Use AVAudioSessionCategoryPlayback
instead.