Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alarm clock like sleep cycle ios swift

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.

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.

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.

My current progress:

  1. 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:

    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 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.

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).

Please help :)

like image 893
Kasper Avatar asked Jan 24 '16 16:01

Kasper


People also ask

How do you set Sleep Cycle alarm on iPhone?

Open the Clock app and tap the Bedtime tab. Under Schedule, tap Bedtime or Wake up. . As you drag, your Bedtime reminder and Wake Up alarm automatically update.

Can iPhone track sleep patterns?

Based on your iPhone usage at night, Sleep on iPhone tracks and charts your Time In Bed. To receive sleep data from Apple Watch, sleep tracking must be enabled for at least 4 hours each night. To view your sleep history, open the Health app on your iPhone, tap Browse at bottom of the screen, then tap Sleep.

Does Sleep Cycle alarm work?

Therefore, though Sleep Cycle is an exciting development in inexpensive technology that may help us to monitor our sleep, it seems that it is really only accurate to track your bedtime and wake time. It should not be used as an alternative to more sophisticated sleep studies.


1 Answers

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

first of all, here is function which helps me to set up a 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 user press a button witch "start alarm" I do that:

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

as you can guess it is sound of nothing - empty sound. Apple said:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

-Apps that play audible content to the user while in the background, such as a music player app

An app that plays or records audio continuously (even while the app is running in the background) can register to perform those tasks in the background. You enable audio support from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the audio value in your app’s Info.plist file.)

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

After that my app is able to run in background without limitations. If Apple won't allow me to publish it I will start using a microphone or something like that. Without that functionality it isn't possible to do alarm clock app.

And changig volume of device is very simple:

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

and you set view.value form 0 - 1.

Hope it will help :)

like image 96
Kasper Avatar answered Nov 07 '22 07:11

Kasper