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

问题描述:

您知道iPhone在睡觉时如何播放闹钟声, 就像iPhone中的内置时钟应用程序一样吗?iPhone - 在后台应用程序中播放闹钟声音

非常重要的修改:在内置时钟应用在iPhone
报警时播放声音时,如果用户切换静音切换到静音(振动模式),
报警声仍继续

玩。

你知道该怎么做吗?

+0

我已经在这个月了太多,检查出[我的线程](http://stackoverflow.com/questions/ 9725192 /怎么办,我启动扮演,音频时,在静音模式锁定功能于IOS-6)。 – 2012-10-08 02:50:27

只有一种方法是使用local NotificationsPush notifications ..both允许30秒

+0

谢谢Shubhank回复,请参阅我的编辑问题! – 2012-02-21 14:42:57

+0

内置的应用程序(本机iOS应用程序)没有第三方iOS应用程序的限制...所以简而言之,如果用户切换到无声,您的应用程序声音将不会播放。 – Shubhank 2012-02-21 14:58:01

+0

但此免费应用程序可以在背景中播放声音,并且在用户切换为静音时不会保持沉默:http://itunes.apple.com/us/app/i-qi-singing-bowl-timer-free/id427428211?mt=8,do你知道他们怎么做到的? – 2012-02-21 16:37:04

设定按钮连接到运行在视图控制器称为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]; 
    } 
} 
+0

感谢Pravi回复,请参阅我的编辑问题! – 2012-02-21 14:42:48

+2

@eeerahul:感谢编辑我的答案和@ Tuyen Nguyen:对不起,我不知道这个,但我认为它无法播放静音模式兄弟声音。如果你真的想要谢谢我,那么点击向上箭头按钮thnx – 2012-08-09 14:05:50

+0

@TuyenNguyen:如果你可以改变声音模式,那么你可以做任何你想做的事情....我想我永远不会发生大声笑。祝你好运 – 2012-08-10 04:39:40

在应用程序进入后台之前,使用AVAudioPlayer播放静音并保持播放次数无限。当你的通知开始时,再次使用AVAudioPlayer播放你的通知声音,而不是UILocalNotification对象。它为我工作。

所以看看i-qi应用程序,他们很可能已经在其info.plist中将其UIBackgroundModes设置为“audio”。这意味着他们能够在后台播放音频。他们可能会在定时器结束前播放无声音频,因为如果不使用后台音频会话将会被切断。

无声开关方面,这可能是基于他们正在使用的会话类别。看看这个资源: http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

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