ANP
ANP

Reputation: 11

ios how to run the alarm sound in background

I am developing an alarm App. At the time while alarm is playing sound, if the app is in open (foreground) then it executes the

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Method & alarm sound plays successfully.

But at time while playing the alarm sound if my app is in inactive/ background state then it not called the above method.

So is there is any way so that i can play alarm sound in background also. For now when my app is in background only the notification is arrive & no sound/song is playing.

Below is my Code

-(void)application:(UIApplication *)application didReceiveLocalNotification(UILocalNotification *)notification
{
        musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

        [musicPlayer setQueueWithItemCollection:collection];
        [musicPlayer play];

        [self showReminder:@"Alarm ON!"];
}

- (void)showReminder:(NSString *)text
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alarm"
                                                        message:text delegate:self
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"Snooze",@"Ok",nil];
    [alertView show];

}

I just wanted to know how can play a song when the app is inactive/background and also show alert to stop / snooze the alarm.

Please help me.

Upvotes: 0

Views: 2283

Answers (2)

Gavin Hero
Gavin Hero

Reputation: 11

Below is my Code,you mabey have a try!

- (void)applicationDidEnterBackground:(UIApplication *)application{
 musicPlayer = [MPMusicPlayerController applicationMusicPlayer];

    [musicPlayer setQueueWithItemCollection:collection];
    [musicPlayer play];

    [self showReminder:@"Alarm ON!"];
 }

Upvotes: 0

Related Questions