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

标签 iphone objective-c background alarm

你知道如何在 iPhone 休眠时播放闹钟声音吗? 喜欢 iPhone 中的内置时钟应用程序吗?

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

你知道怎么做吗?

最佳答案

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

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

关于iPhone - 在应用程序处于后台时播放闹钟声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9372800/

相关文章:

iphone - 将导航栏中的按钮居中

ios - 不同国家的联系地址格式

ios - 按键盘上的返回键时的 Action (iOS)

iphone - cocos2d-x 创建 CCMenuItem 报错

ios - 将 iOS 应用程序从一个 Apple 帐户转移到另一个

objective-c - 如何验证从队列返回的内容与其预期的 UITableView 单元格匹配?

iphone - 调用 graph api Facebook ios 时 facebookErrDomain 错误 10000

ios - PhoneGap iOS getCurrentPosition while app is in background

html - 谁能找到这个空白是从哪里来的?

background - VML 后台代码中断对齐 Outlook 电子邮件表