0

I'm trying to build an alarm app. To play alarms when the app is in the background, my app plays a completely silence sound (however I'm hearing a quiet noise, although if I play the same sound file on my computer there is definitely nothing to hear) in the background until the alarm time comes and the AVPlayer plays the alarm sound. It works, but when my app plays the silent sound in the background, some other apps (especially some games) don't play their sound. This is kind of weird, because there is the app 'Alarmy' in the AppStore, which definitely uses the same system with a silent sound, but when I set an alarm in this app, it doesn't silence any other sound.

How can I achieve this? Currently I'm setting my AVAudioSession like this (I'm setting it in the initializer of my alarm player class, of which i have an instance in my AppDelegate):

let session = AVAudioSession.sharedInstance()
do {
    try session.setCategory(AVAudioSessionCategoryPlayback, with: .mixWithOthers)
    try session.setActive(true)
} catch {
    print("Failed to set audio session category.  Error: \(error)")
}

EDIT: For more explanation about the app Alarmy, look also at App "Alarmy" is able to play iTunes song from background state... How?

Josef Zoller
  • 921
  • 2
  • 8
  • 24

1 Answers1

1

"To play alarms when the app is in the background, my app plays a completely silence sound." Do you mean you're trying to use playing silence to keep your app alive? This isn't allowed. If you've found other apps that do this (how do you know they're doing it that way?), that doesn't mean it's allowed. The fact that someone has gotten away with something doesn't change AppStore rules (and doesn't change the fact that silent audio is a major battery drain).

The correct way to build an app that launches at a specific time to play an alarm is with a UNNotificationRequest. You can register the notification to wake you up at a given date and time and you can perform your alarm then, or (often better) just have the notification take care of the alarm without waking you up until the user taps on the message.

See the Local and Remote Notification Programming Guide for full details.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • The problem with UNNotificationRequest is, that it doesn't work in silent or do not disturb mode. I know, that the app which I mean uses this method, because it works in this modes and also because the app bundle contains a silent soundfile. Probably they got the app in the AppStore, because the app also plays music for falling asleep. – Josef Zoller May 19 '18 at 19:18
  • For more explanation look https://stackoverflow.com/questions/22823126/app-alarmy-is-able-to-play-itunes-song-from-background-state-how – Josef Zoller May 19 '18 at 19:25
  • Is it possible to wake the app when a local notification is triggered (if the app in the background state)? – Nimesh Chandramaniya Jun 11 '19 at 11:21
  • "You can register the notification to wake you up at a given date and time and you can perform your alarm then ..." <- How is this possible with a local notification? Can you please give more details? – Luke Mar 29 '20 at 20:49