IOS 8,开始播放声音的背景下,闹钟应用程序时 [英] iOs 8, start playing sound when in the background, alarm clock app
问题描述
我知道有上SOF很多问题,但这是最新之一。关键是,我试图创建和报警的应用程序,我知道一个事实,即有报警应用程式在那里,完美的作品(在某种程度上),即使应用程序没有运行,并在后台。
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 out there that works perfectly ( somehow ), even if the app is not running and is in the background.
我的问题是:你是怎么开始播放声音的在您的应用程序已经在后台?
My question is: how do you start playing sound after your app is already in the background ??
UILocalNotification是伟大的,但你只能得到应用程序:(_:didReceiveLocalNotification:)
所需的背景模式:应用程序播放音频或数据流通过AirPlay的音频/视频的info.plist
已设置。一旦音乐开始播放,它让玩即使我去后台。
UILocalNotification is great, but you'll only get application:(_:didReceiveLocalNotification:)
after the user has already clicked on your notification, so playing sound using AVAudioPlayer didn't work, i want to play sound regardless weather the user clicks on it or doesn't. Of course with Required background modes: App plays audio or streams audio/video using AirPlay
in info.plist
already set. Once the music starts playing, it keeps playing even if i go to the background.
我不希望使用UILocalNotificaation声音怎么把它限制在30秒,只有能够发挥被捆绑与应用程序的声音。
I don't want to use UILocalNotificaation sound coz it's limited to 30 secs and only the able to play the sounds that are bundled with the application.
任何见解?想法?
样code:
var notif = UILocalNotification()
notif.fireDate = self.datePicker.date
notif.alertBody = "test"
UIApplication.sharedApplication().scheduleLocalNotification(notif)
当用户选择并报警,并点击保存上述code被调用。
the above code gets called when the user selects and alarm and clicks save.
和这里发生的事情在AppDelegate中:
and here's what's happening in 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
顺便说一句,这样就不会得到释放,而 audioplayer.play()
不会做任何事情...
You need to hold a strong reference to the AVAudioPlayer
btw, so it won't get released, and the audioplayer.play()
won't do anything...
推荐答案
最后,我设法用的NSTimer做。
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()
}
和其他地方一个UILocalNotification已同步与此计时器,一个良好的用户体验。
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 :\
这篇关于IOS 8,开始播放声音的背景下,闹钟应用程序时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!