我知道SOF有很多问题,但这是“最新”的一个。事情是,我正在尝试创建和报警应用程序,而且我知道有一个事实,即有应用程序没有运行并且位于后台的警报应用程序完美工作(以某种方式)。iOs 8,在后台开始播放声音,闹钟应用程序
我的问题是:你如何开始播放声音后你的应用程序已经在后台?
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)
当用户选择和报警和点击保存上述代码被调用。
,这里是正在发生的事情的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")
}
你需要保持一个强大的参考AVAudioPlayer
BTW,所以它不会得到释放,而audioplayer.play()
不会做什么...
如果您的应用程序在后台,'didreceivelocalnotification'应后,被称为通知被解雇。也许你可以把你的代码放在那里播放声音。 – Chris
我尝试过很多次了,它不会,只有在用户实际点击通知后才会被触发。无论如何,我会编辑我的问题。 – Nour1991