iPhone - 在后台应用程序播放闹钟声音

时间:2012-02-21 05:46:38

标签: iphone objective-c background alarm

你知道如何在iPhone睡觉时播放闹钟声, 像iPhone中的内置时钟应用程序?

非常重要的编辑:
在iPhone中的内置时钟应用程序中 当播放警报声时,如果用户将静音开关切换到静音(振动模式),则 警报声仍然继续播放。

你知道怎么做吗?

5 个答案:

答案 0 :(得分:3)

此代码将帮助您在后台播放音乐: 音乐的声音文件应该只有30秒 通知支持30秒声音文件,它应该在捆绑文件夹中,如果它在文件夹中,那么你必须放置声音文件的路径

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];

答案 1 :(得分:2)

设置按钮已连线以在视图控制器中运行名为scheduleNotification的方法,该方法使用UILocalNotification类来安排通知。代码如下:

(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];
    }
}

答案 2 :(得分:1)

在应用程序进入后台之前,使用AVAudioPlayer播放静音,并保持无限次播放。当您的通知启动时,再次使用AVAudioPlayer播放您的通知声音,而不是UILocalNotification对象。它对我有用。

答案 3 :(得分:1)

所以看看i-qi app,很可能他们已经在他们的info.plist中将他们的UIBackgroundModes设置为“audio”。这意味着他们可以在后台播放音频。他们可能会播放无声音频,直到计时器结束,因为如果不使用背景音频会话将会被切断。

就静默切换而言,这可能基于他们正在使用的会话类别。看看这个资源: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

答案 4 :(得分:0)

只有方法是使用local NotificationsPush notifications ..两种方式都允许播放声音30秒

相关问题