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

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

你知道怎么做吗?

最佳答案

此代码将帮助您在后台播放音乐:
音乐的声音文件应该只有 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];

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

10-14 21:56
查看更多