2011-07-01 65 views
5

我正在尝试构建类似于当前在应用商店中的闹钟专业版和床头柜应用程序的闹钟。当闹铃时间到达时(通常是第二天早上),这些应用程序中的每一个都能够播放超过30秒钟的闹钟声。如何像闹钟专业版应用程序一样播放超过30秒的闹钟声音?

我已经试过两种方法已经没有运气:

方法1:

[self performSelector:@selector(playAlarm) withObject:nil afterDelay:myDouble]; 

方法2:

  UILocalNotification *notif = [[cls alloc] init]; 
    notif.fireDate =[datePicker date];//firedate; 
    notif.timeZone = [NSTimeZone systemTimeZone]; 

    notif.alertBody = @"Time to wake up!"; 
    NSString *SoundFileName=nil; 
    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"ActualSoundFile"] isKindOfClass:[NSString class]]) 
     SoundFileName=[[[NSString alloc]initWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"ActualSoundFile"]]autorelease]; 
    else 
     SoundFileName=[[[NSString alloc] initWithString:@""] autorelease]; 

    if([SoundFileName length]>1) 
     notif.soundName = [SoundFileName stringByAppendingString:@".wav"]; 
    else 
     notif.soundName = UILocalNotificationDefaultSoundName; 

    [email protected]"Snooze"; 
    notif.repeatCalendar=[NSCalendar currentCalendar]; 
    notif.repeatInterval =NSDayCalendarUnit; 

    NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"Alarm" forKey:kRemindMeNotificationDataKey]; 

      notif.userInfo = userDict; 

    [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
    [notif release]; 

有谁知道他们是怎么能够在7小时后在循环播放闹钟?

+0

对于方法2,是所有的代码?在一行之后你肯定需要更多的代码。 –

+0

@Ben - 刚刚添加了两种方法。 – daSn0wie

回答

1

你需要通过指定日期到fireDate财产火灾本地通知和声音文件分配到

UILocalNotification *localNotif = [[[UILocalNotification alloc] init]autorelease]; 
localNotif.fireDate = scheduleDate; 
NSLog(@"fireDate is %@",localNotif.fireDate); 
localNotif.timeZone = [NSTimeZone defaultTimeZone]; 
localNotif.alertBody = @"WAKE UP...!!!"; 
localNotif.alertAction = @"View"; 
localNotif.soundName = @"Default.wav"; 


[[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 

这样,即使应用程序被关闭本地通知将被解雇,记住“Default.wav”文件应该小于或等于30秒,即使闹钟专业版app在本地通知中播放声音= 30秒。

如果应用程序是活的,可以实现的appdelegate的委托方法,并能应用你的逻辑,显示警报视图和播放声音甚至>30秒.....

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
} 
+0

你如何保持应用程序'活着'?当我离开我的应用程序运行并进入睡眠状态时,声音只播放30秒。闹钟应用程序会播放声音,直到我实际醒来并执行用户操作。 – daSn0wie

-2

所以我想我找到一个有效的解决方案:

为了模拟报警声音播放时间超过30秒,刚添加多个localnotifications一前一后,相隔30秒。

+1

这不是正确的答案,因为用户可能会在第一次通知期间醒来并选择关闭它。猜猜看,第二个通知会给用户一个警报已被打破的印象。 – MyCSharpCorner

+0

这需要由应用程序处理。一旦选择了警报,您需要取消所有其他本地通知 – daSn0wie

+1

这不是一个正确的解决方案 –

5

选择的答案是不正确的答案,因为用户可以在第一通知中醒来,并选择将其关闭。猜猜看,第二个通知会给用户一个警报已被打破的印象。

根据应用程序的文档正确的答案如下:

当您的通知到达时不能播放声音超过30秒,而你的应用程序在后台运行(例如,用户在睡觉前关闭应用程序)。

要播放一个较长的声音,你一定要告诉你的用户在睡觉前离开在前台的闹钟应用,然后在didReceiveLocalNotification您实现打更长的人工声音。

+0

这是不正确的。您可以以30秒的间隔连续执行相同的声音次数。编写应用程序的人需要清除用户参与后设置的任何连续通知。 – daSn0wie

+0

当系统在您的应用程序终止或在后台暂停时显示您的通知时,有两个选项:“关闭”和另一个按钮。点击关闭按钮不会通知您的应用程序,因此无法像您说的那样清除任何连续的通知。因此,即使用户每次选择“关闭”,用户仍会继续收到计划的30秒钟警报。在我看来,这是一个相当混乱的用户体验。 – MyCSharpCorner

+0

您可以将用户带回您的应用程序,并执行:[[UIApplication sharedApplication] cancelAllLocalNotifications]; – daSn0wie