title: Playing a sound while app is in the foreground and the screen is locked - iOS
body: <p>Preface: I'm building an alarm clock app. Many other alarm apps such as Alarm Clock Pro are able to play an alarm while the screen is locked and the app is in the foreground. Their alarms can play for an unlimited amount of time and can progressively increase in volume using the system volume. They also do not take control of the music controls (if you open up the multitasking screen and scroll to playing audio, you will not see their icon)</p>
  
  <p>I am having some trouble reproducing that functionality.</p>
  
  <p>To play alarms while the app is in the foreground we fire Local notifications which works great.  I've have some limited success  while the screen is locked (the and the app is in the Inactive state)</p>
  
  <p>I've used the following methods:</p>
  
  <ol>
  <li><p>Run an <code>NSTimer</code> every second with a background task when the screen is locked. I managed to keep the app open past the 10 minute maximum but I was unable to play a sound. when the time came</p></li>
  <li><p>Play a 1 second silent sound using <code>AVAudioPlayer</code>. When the sound ends, replay the sound and check if the app is in the Inactive (locked screen) state. If it is in the locked screen state and it's time to play an alarm, play it. The problem here is I must use</p>
  
  <p><code>[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];</code>   </p></li>
  </ol>
  
  <p>in order to continue updating the song while it's in the background. That method will display Wake in the music controls and will stop any currently playing music which we want to avoid.</p>
  
  <ol start="3">
  <li>A combination of #1 and #2. When the app opens play a silent sound on infinite repeat. When the screen is locked fire a timer to check if its time to play an alarm. When it's time to play an alarm, switch the silent sound to the alarm sound. The system would eventually force close the app</li>
  </ol>
  
  <p>Related knowledge that has helped but hasn't solved my question:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/9725192/how-do-i-start-playing-audio-when-in-silent-mode-locked-in-ios-6">How do I start playing audio when in silent mode &amp; locked in iOS 6?</a></li>
  <li><a href="https://stackoverflow.com/questions/8561504/ios-5-deep-sleep-prevention">iOS 5 deep sleep prevention</a></li>
  <li><a href="https://stackoverflow.com/questions/10406874/resuming-execution-of-code-after-interruption-while-app-is-in-background">Resuming execution of code after interruption while app is in background</a> </li>
  <li><a href="https://stackoverflow.com/questions/11843326/how-to-play-music-in-background-iphone-sdk?rq=1">How to play music in background iphone sdk</a></li>
  </ol>
  
  <p><strong>Summary: While the screen is locked and in the app is in the foreground, I am unable to prevent the process from being killed off after the 10 minute mark.</strong></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I ended up using <a href="https://github.com/mruegenberg/MMPDeepSleepPreventer" rel="nofollow noreferrer">https://github.com/mruegenberg/MMPDeepSleepPreventer</a> which let me play a sound after 10 minutes. However, this eats up battery like crazy. I need to find a more efficient solution.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I downloaded some of <a href="http://marcopeluso.com/" rel="nofollow noreferrer">http://marcopeluso.com/</a> apps from the app store. He is the creator of the Deep Sleep Preventer. I downloaded some of his apps and ran some instruments tests and saw his app was not draining battery as much as my app is (roughly 2.5% per hour as claimed somewhere on his blog). I am very certain that I need to optimize my app and the battery drain problems will disappear and my problems will be solved!</p>
  
  <p><strong>UPDATE 3</strong>
  I ended up using <a href="https://github.com/mruegenberg/MMPDeepSleepPreventer" rel="nofollow noreferrer">https://github.com/mruegenberg/MMPDeepSleepPreventer</a></p>
  
  <p>I ran it in it's own separate xCode project and it only uses 0.5% of the cpu when the app is in the background. It turns out it was my app was sucking the cpu. So all it working well now :)</p>