title: alarm clock like sleep cycle ios swift
body: <p>I have been looking for answer very long and I haven't found it. I'd like to create some app which would be similar to alarm clock. </p>
  
  <p>One of it's features will be waking up at specified by the user time (nothing surprising). If you look at sleep cycle app, you would notice that it wakes you up but it also track your sleep, so it has to be running at background. Moreover it also can play song which wakes you up until you turn it off (not only for 30 seconds, as the limit of length of notification sound). It also can turn up the volume of device. </p>
  
  <p>If I haven't seen this app at action I wouldn't believe that such a functionality is possible on iPhone to do by developers.</p>
  
  <p>My current progress:</p>
  
  <ol>
  <li><p>I can play the sound at time specified by user but only if the app is in foreground. If sound is played and then user click home button sound is still played (that is cool) but the music can't start if the app is in background. This is some code:</p>
  
  <pre><code>do {
          try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
          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)
      }</code></pre>
  
  <p>and then I use <pre><code>AVAudioPlayer</code></pre> to play some sound.
  So the first question is: how to play that sound from background like sleep cycle app does? And I'm sure that sleep cycle doesn't use notification sound.</p></li>
  </ol>
  
  <p>And my second question is how to change device volume (sleep cycle also can do it but on the stack overflow there are many people saying that it is impossible).</p>
  
  <p>Please help :)</p>