Alarm clock such as ios swift sleep cycle

I searched for an answer for a very long time, and I did not find it. I would like to create an application that will look like an alarm clock.

One of its functions will wake up at the time specified by the user (nothing surprising). If you look at the sleep cycle app, you will notice that it wakes you up, but it also tracks your sleep, so it should work in the background. In addition, it can also play a song that wakes you up until you turn it off (not only for 30 seconds, as the limit on the length of the notification sound). It can also increase the volume of the device.

If I did not see this application in action, I would not believe that such an opportunity is possible on the iPhone for developers.

My current progress:

  • I can play sound at a time specified by the user, but only if the application is in the foreground. If the sound is playing, and then click on the home button, the sound is still playing (this is cool), but the music cannot start if the application is in the background. This is the 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) } 

    and then i use

     AVAudioPlayer 
    to play some sound. So, the first question: how to reproduce this sound from the background, like a sleep cycle application? And I'm sure the sleep cycle does not use notification sound.

And my second question is how to change the volume of the device (a sleep cycle can also do this, but when the stack overflows, many people say that this is impossible).

Please, help:)

+6
source share
1 answer

OK, so I managed to do this using some tricks:

First of all, here is the function that helps me set up the audio player:

 func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? { //1 let path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String) let url = NSURL.fileURLWithPath(path!) //2 var audioPlayer:AVAudioPlayer? // 3 do { try audioPlayer = AVAudioPlayer(contentsOfURL: url) } catch { NSLog("Player not available") } return audioPlayer } 

then when the user clicks the “trigger alarm” button, I do this:

 silence_audio = setupAudioPlayerWithFile("silence", type:"wav"); silence_audio?.numberOfLoops = -1; silence_audio?.volume = 1; silence_audio?.play(); 

as you can guess, this sound is nothing - an empty sound. Apple said:

For tasks that require longer execution time for implementation, you must request specific permissions to run them in the background without pausing them. On iOS, only certain types of applications are allowed to run in the background:

-Apps that play audio content to the user in the background, for example, an application for a music player

An application that plays or records audio continuously (even while the application is running in the background) can register to perform these tasks in the background. You enable sound support from the Backgrounds section of the Features tab in your Xcode project. (You can also enable this support by including a UIBackgroundModes key with an audio value in your application Info.plist file.)

And I also had to do this: enter image description here

After that, my application can run in the background without restrictions. If Apple does not allow me to publish it, I will start using a microphone or something like that. Without this functionality, it is impossible to make an alarm application.

And too large a device’s volume is very simple:

  let volumeView = MPVolumeView() if let view = volumeView.subviews.first as? UISlider{ view.value = 0.3 } 

and you set the form view.value 0 - 1.

Hope this helps :)

+2
source

All Articles