Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 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.

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.

Any insights ? thoughts ??

Sample code:

    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.

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")


}

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...

like image 878
Nour Helmi Avatar asked Jul 15 '14 02:07

Nour Helmi


People also ask

How do I get my alarm to say the time iPhone?

Go to settings>general>accessibility and right at the bottom go to accessibility shortcut and choose VoiceOver. Press the home button. Now to switch VoiceOver on just triple tap the home button, to switch it off triple tap the home button.

Does microsoft alarm work when laptop is closed?

Alarms and timers work even if the app is closed or your device is locked. If you see a warning that notifications only show if the device is awake, make sure your device doesn't go to sleep (Go to Settings > System > Power & sleep to adjust your device's sleep settings).


1 Answers

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 :\

like image 134
Nour Helmi Avatar answered Nov 10 '22 20:11

Nour Helmi