5

Many alarm apps managed to play sound with locked screen after several hours (examples: Rise, Wave alarm clock)

All you need to do is set the alarm and lock your screen while the app is open and an alarm will go off hours after the screen has been locked.

I managed to figure out how to play audio with AVPlayer while my device is locked:

  1. Set background capabilities to audio

  2. Set 'Application does not run in background' to YES

  3. Code:

    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        print("AVAudioSession Category Playback OK")
    do {
        try AVAudioSession.sharedInstance().setActive(true)
        print("AVAudioSession is Active")
        } catch let error as NSError {
            print(error.localizedDescription)
        }
    } catch let error as NSError {
        print(error.localizedDescription)
    }
    

When I try to set a NSTimer or use dispatch_after to run the code that plays my audio file and lock my iPhone the Timer stops pretty quickly because apparently the app gets inactive after just a few seconds (But only when I start the App from the home screen and doesn't when I run the App from xcode, btw)

How do those alarm clock apps manage to delay audio?

3
  • Local notifications, probably Jan 12, 2017 at 22:47
  • nope. local notifications are limited to sounds of 30 seconds max and also they don't ring when silent but those apps do Jan 12, 2017 at 22:57
  • They could put the alarm to your Calendar (event of type alarm).
    – Sulthan
    Jan 13, 2017 at 8:41

2 Answers 2

1

These apps achieve this by opting out of the multi-tasking mode. If an app opts out of multi-tasking mode, the app is not suspended if the app was in the foreground when the device got locked and hence they are able to play the alarm sound when the time arrives. You can read more here

-1

(Posted answer on behalf of the question author to move it to the answer space).

I figured out how they do it. I set an alarm and listened to the speakers. Those apps play silent sound to stay active. I don't know how they passed the checks by Apple but apparently many alarm clock apps do this and pass.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.