I regularly run the background thread scheduledTimerWithTimeInterval() , which checks for records in the database with a certain status

If there are such records, then you need to play mp3.

Did playback using:

1. AVAudioPlayer

Enabled Audio, AirPlay and Picture in Picture в BackgroundModes

  do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) } catch _ { return print("error") } 

Set category AVAudioSessionCategoryPlayback

Problem: a sound is played every 20 seconds on the open screen (music plays), but when I just turn off the application, the sound itself is no longer playing.

2. AudioServicesPlayAlertSound (1009)

It worked but APPLE was rejected with the reason " backgroundModes mode is on but nothing is played."

What can be used for such a task?

UPD

This is the method that starts the timer at intervals of 20 seconds:

 func scheduledTimerWithTimeInterval(){ timer = NSTimer.scheduledTimerWithTimeInterval(20, target: self, selector: #selector(ListOfOrders.checkNewOrder), userInfo: nil, repeats: true) registerBackgroundTask() } 

This is the method that starts the timer in which the data is downloaded and validated. If there is new data, in theory the phone should squeak

 func checkNewOrder() { WorkWithInternet().getOrders(self, token: prefs.stringForKey("token")!, col: 0, add: false, vc: ListOfOrders()) } 

UPD2 Play sound by file:

 do { let soundURL: NSURL = NSBundle.mainBundle().URLForResource("notif", withExtension: "mp3")! audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL) audioPlayer!.delegate = self audioPlayer!.prepareToPlay() audioPlayer!.play() }catch{ print("Error") } 

UPD3

 var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid 

Register a task in the background:

 func registerBackgroundTask() { backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { [unowned self] in self.endBackgroundTask() } assert(backgroundTask != UIBackgroundTaskInvalid) } 

Background termination task function:

 func endBackgroundTask() { NSLog("Background task ended.") UIApplication.sharedApplication().endBackgroundTask(backgroundTask) backgroundTask = UIBackgroundTaskInvalid } 
  • Similar questions on enSO do not help? - Denis
  • Yes, it is enough to search for the same words that you have in the title) I found it like this. - Denis
  • @Denis, did not help. This is not what is needed, I have this code in the project too. That block of code is needed to continue playing the sound in the background, if it came out of activating the application, but I need the sound to start playing when the application is already minimized, for example, after 20 seconds. By the way, according to your 2nd comment. Naturally) at enSO also asked this question. And there, too, silence - zayn1991
  • show the code how you run every 20 seconds - Max Mikheyenko
  • make a playlist so that after one go empty tracks of 20 seconds long, or again make a playlist so that each track has 20 seconds of silence and then your sound is at the end - Max Mikheyenko

1 answer 1

Take a look at this project from the book.

I personally tested it on iOS 9.3 iPhone 6+ everything works.