0

I am making an alarm clock app. To play the alarm sound at the appropriate time, I use myAudioPlayer.play(atTime: myAudioPlayer.deviceCurrentTime + secondsUntilAlarm). This way, even if the app is in the background, the audio player plays the alarm sound at the appropriate time.

Note: I got this idea from a different SO answer, which unfortunately I can't seem to find right now.

However, what I've noticed is that alarms are being played correctly if the secondUntilAlarm value is relatively soon, like maybe 20 minutes or less (converted to seconds of course since that's what the method requires). However, if it's longer than that, the sound does not play. Is there something I'm missing with how this method works in the background? Could the app be entering some sort of suspended state or something that disables the audio player from triggering the playback?

Any suggestions or comments would be appreciated--thanks!

Eric
  • 569
  • 4
  • 21
  • 2
    Yes, the app is definitely being suspended. Apps on iOS don’t just run forever in the background. You’ll have to use something like a local notification. – jnpdx Aug 26 '21 at 18:15
  • @jnpdx I've realized the function still works while the app is in the suspended state! Just had to add `mixWithOthers` so my audio session was not deactivated . – Eric Aug 28 '21 at 17:51

1 Answers1

0

So I've actually determined that it does not matter if the app enters the suspended state (in fact, this is expected). I've identified that the reason the alarm sound does not play sometimes because I open another app with audio that deactivates my app's audio session (therefore, it has nothing to do with the time the alarm is set for). To fix this, all I had to do was ensure I set .mixWithOthers for my app's audio session. That way, other audio sessions from other apps don't deactivate mine!

Eric
  • 569
  • 4
  • 21
  • how did you manage to fix that? I am facing similar problem. Whenever my phone is silent and I get a call on my phone, my alarm does not play. I have set the category of `AVAudioSession` to `.playback` and also options to `.mixWithOthers` but my the sound is not played at all. – Shoaib A Aug 22 '22 at 13:05
  • @ShoaibA Check out my [other SO post](https://stackoverflow.com/questions/68684957/local-notification-sound-not-playing-while-app-in-background-swift) and see if it helps at all. Otherwise, you might want to ask it in an independent post because I think I need a little more info on your question. – Eric Aug 23 '22 at 05:36
  • perhaps I need to open another thread because the main problem I am facing is, my AudioSession is being deactivated ONLY in case of incoming calls. I had already managed to fix the issue where apps like YouTube tend to deactivate my app's session. Did you face a smiliar probelm with incoming calls as well? – Shoaib A Aug 23 '22 at 05:46
  • @ShoaibA Well 1/100 times I noticed my alarm didn't play, but I could never figure out / reproduce the issue. Perhaps it was because of an incoming call? I don't think I analyzed that case so I'm not sure the solution. – Eric Aug 23 '22 at 18:48