iOs 8,在后台开始播放声音,闹钟应用 [英] iOs 8, start playing sound when in the background, alarm clock app

查看:24
本文介绍了iOs 8,在后台开始播放声音,闹钟应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于 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 很棒,但是您只会在用户点击您的通知后获得 application:(_:didReceiveLocalNotification:),所以使用 AVAudioPlayer 播放声音不起作用,我想要无论用户点击或不点击,播放声音.当然,必需的后台模式:应用程序在 info.plist 中使用 AirPlay 播放音频或流音频/视频已经设置.一旦音乐开始播放,即使我转到后台,它也会继续播放.

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.

任何见解?想法??

示例代码:

    var notif = UILocalNotification()
    notif.fireDate = self.datePicker.date
    notif.alertBody = "test"
    UIApplication.sharedApplication().scheduleLocalNotification(notif)

当用户选择并报警并点击保存时,将调用上述代码.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆