iOs 8, start playing sound when in the background, alarm clock app

前端 未结 1 1748
不思量自难忘°
不思量自难忘° 2020-12-13 01:22

I know there are many questions on SOF, but this is the \"newest\" one. The thing is, I\'m trying to create and alarm app, and I know for a fact that there are alarm apps ou

相关标签:
1条回答
  • 2020-12-13 01:33

    Finally, I managed to do it with NSTimer.

    func applicationWillResignActive(application: UIApplication) {
        var timer = NSTimer(fireDate: NSDate(timeIntervalSinceNow: 20), interval: 60.0, target: self, selector: Selector("playAlarm"), userInfo: nil, repeats: false)
        NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode)
    }
    func playAlarm() {
        self.sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "caf"))
        self.audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
        audioPlayer.play()
    }
    

    and somewhere else a UILocalNotification is synced with this timer, for a good user experience.

    Although I'm not sure if this would go through apple, but I don't see any reason why it wouldn't :\

    0 讨论(0)
提交回复
热议问题