Tuyen Nguyen
Tuyen Nguyen

Reputation: 4479

iPhone - play alarm sound while the app in background

Do you know how to play an alarm sound when the iPhone is sleeping, like the built-in Clock app in iPhone?

VERY IMPORTANT EDIT:
in the built-in Clock app in iPhone
when the alarm sound is playing, if user switch the Silent switch to Silent (Vibrate mode),
the alarm sound still continue to play.

Do you know how to do the same?

Upvotes: 3

Views: 6551

Answers (5)

Jaspreet Singh
Jaspreet Singh

Reputation: 1180

this code will help you to play music in background: the sound file of the music should be only 30 sec notification support 30 sec sound file and it should be in the bundle folder if it is in the document folder then you have to put the path of the sound file

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSString *dateValueRemaining =[NSString stringWithFormat:@"23/08/2012 11:30:33"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *dateRemaining = [dateFormat dateFromString:dateValueRemaining];
NSDate *pickerDate = dateRemaining;
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit
                                                         | NSMonthCalendarUnit
                                                         | NSDayCalendarUnit)
                                               fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit
                                                         | NSMinuteCalendarUnit
                                                         | NSSecondCalendarUnit)
                                               fromDate:pickerDate];
// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"itemDate: %@", itemDate);
if (localNotif) {
    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) {
    return;
}
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = itemDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"Your Time is Over";
localNotif.alertAction = @"View";
//---THIS SONG FILE IN THE NSBUNDLE FOLDER---------//
localNotif.soundName = @"s1.mp3";
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

Upvotes: 3

nvrtd frst
nvrtd frst

Reputation: 6332

So looking at i-qi app, most likely they've set their UIBackgroundModes to "audio" in their info.plist. This means that they're able to play audio in the background. They're probably playing silent audio until the timer ends because background audio sessions will get cut if they're not in use.

In terms of the silent switch, thats probably based on the session category they're using. Check out this resource: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

Upvotes: 1

Sterling
Sterling

Reputation: 161

Before the app enters the background, play a silent sound with AVAudioPlayer and keep it play infinite number of times. And when your notification kicks in, again use AVAudioPlayer to play your notification sound, not the UILocalNotification object. It worked for me.

Upvotes: 1

Prabhjot Singh Gogana
Prabhjot Singh Gogana

Reputation: 1408

The set button is wired up to run a method called scheduleNotification in the view controller which uses the UILocalNotification class to schedule a notification. The code looks as follows:

(void)scheduleNotification
{
    [reminderText resignFirstResponder];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
    {
        UILocalNotification *notif = [[cls alloc] init];
        notif.fireDate = [datePicker date];
        notif.timeZone = [NSTimeZone defaultTimeZone];
        notif.alertBody = @"Did you forget something?";
        notif.alertAction = @"Show me";
        notif.soundName = UILocalNotificationDefaultSoundName;
        notif.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
              forKey:kRemindMeNotificationDataKey];
        notif.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notif];
        [notif release];
    }
}

Upvotes: 2

Shubhank
Shubhank

Reputation: 21805

Only method is to use local Notifications or Push notifications..both allow sounds to be played for 30 seconds

Upvotes: 0

Related Questions