简体   繁体   中英

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

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: 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM