Nou*_*991 13 audio alarm ios swift
我知道SOF有很多问题,但这是"最新的"问题.问题是,我正在尝试创建和报警应用程序,我知道有一个事实,那里有完美(不知何故)工作的警报应用程序,即使应用程序没有运行,并在后台.
我的问题是:你的应用程序已经在后台后如何开始播放声音?
UILocalNotification非常棒,但只有application:(_:didReceiveLocalNotification:)在用户点击了您的通知后才能获得,因此使用AVAudioPlayer播放声音不起作用,我想播放声音,无论用户点击它还是没有.当然已经Required background modes: App plays audio or streams audio/video using AirPlay在info.plist已经设定.一旦音乐开始播放,即使我去了背景,它也会继续播放.
我不想使用UILocalNotificaation声音,因为它限制在30秒内,只能播放与应用程序捆绑在一起的声音.
任何见解?想法?
示例代码:
var notif = UILocalNotification()
notif.fireDate = self.datePicker.date
notif.alertBody = "test"
UIApplication.sharedApplication().scheduleLocalNotification(notif)
Run Code Online (Sandbox Code Playgroud)
当用户选择并报警并单击保存时,将调用上面的代码.
以下是AppDelegate中发生的事情:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
NSLog("launched")
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert |
UIUserNotificationType.Badge, categories: nil
))
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
self.sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("sound", ofType: "caf"))
self.audioPlayer = AVAudioPlayer(contentsOfURL: sound, error: nil)
return true
}
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!){
audioPlayer.play()
NSLog("received local notif")
}
Run Code Online (Sandbox Code Playgroud)
你需要对AVAudioPlayerbtw 有一个强烈的引用,所以它不会被释放,并且audioplayer.play()不会做任何事......
Nou*_*991 10
最后,我设法用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()
}
Run Code Online (Sandbox Code Playgroud)
在其他地方,UILocalNotification与此计时器同步,以获得良好的用户体验.
虽然我不确定这是否会通过苹果,但我没有看到任何理由为什么它不会:
| 归档时间: |
|
| 查看次数: |
9899 次 |
| 最近记录: |