2011-07-01 62 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