3

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?

Tuyen Nguyen
  • 4,389
  • 7
  • 51
  • 77
  • I've been on this for months too, check out [my thread](http://stackoverflow.com/questions/9725192/how-do-i-start-playing-audio-when-in-silent-mode-locked-in-ios-6). – Andres Canella Oct 08 '12 at 02:50

5 Answers5

3

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];
derpoliuk
  • 1,756
  • 2
  • 27
  • 43
Jaspreet Singh
  • 1,180
  • 2
  • 12
  • 30
2

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];
    }
}
eeerahul
  • 1,629
  • 4
  • 27
  • 38
Prabhjot Singh Gogana
  • 1,408
  • 1
  • 14
  • 39
  • 2
    @eeerahul :thanks for edit my answer and @ Tuyen Nguyen : Sorry bro i dont know about this but i think it cant play sound in silent mode bro. and if you really want thank me then click to up arrow button thnx – Prabhjot Singh Gogana Aug 09 '12 at 14:05
  • @TuyenNguyen : one thing more if you can change mode of sound then you can do what ever you want....i think i will never happen lol. best of luck – Prabhjot Singh Gogana Aug 10 '12 at 04:39
1

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

nvrtd frst
  • 6,272
  • 4
  • 28
  • 34
1

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.

Sterling
  • 161
  • 9
0

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

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • the built in apps(native iOS apps) do not have the limitations that 3rd party iOS apps have...so in short ..your apps sound won't be played if user switches to silent.. – Shubhank Feb 21 '12 at 14:58
  • but this free app can play sound when it's in background and won't silent when user switches to silent: http://itunes.apple.com/us/app/i-qi-singing-bowl-timer-free/id427428211?mt=8, do you know how can they do it? – Tuyen Nguyen Feb 21 '12 at 16:37
  • don't know..although i will suggest to make a sample project and use local notification to check for yourself..will take only 15 minutes.. – Shubhank Feb 21 '12 at 16:53
  • Thank Shubhank for your continuous support. Actually I just did, and my UILocalNotification sound (26 seconds) stops right away (while playing) after I turned the switch to silent mode. There must be something else that the i-iQ app do that prevent the sound from stop playing. I'll be grateful if you could show me how. Thanks. – Tuyen Nguyen Feb 21 '12 at 16:59
  • don't know man..as much i know this can't be done..if one press to silent..even the ringtone does not play..how will the i-IQ app can play??(crazy thing!!) – Shubhank Feb 21 '12 at 17:34