Make the following changes in xCode project settings as well as in code.
step 1) Select your project file in the Navigator of Xcode. Then, from the Capabilities section, switch on the Background Modes subsection. After the list of background modes is given to you, tick on the Audio & Airplay switch.
stp 2) Use following swift code, basically you need to set audio session for your app.
var audioPlayer : AVAudioPlayer!
@IBAction func playButtonClicked(sender : AnyObject){
let dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(dispatchQueue, {
if let data = NSData(contentsOfFile: self.audioFilePath())
{
do{
let session = AVAudioSession.sharedInstance()
try session.setCategory(AVAudioSessionCategoryPlayback)
try session.setActive(true)
self.audioPlayer = try AVAudioPlayer(data: data)
//self.audioPlayer.delegate = self
self.audioPlayer.prepareToPlay()
self.audioPlayer.play()
}
catch{
print("\(error)")
}
}
});
}
func audioFilePath() -> String{
let filePath = NSBundle.mainBundle().pathForResource("mySong", ofType: "mp3")!
return filePath
}
This audio playback session will be play your application playback, even if app is in background or phone is in silent mode or device is locked.