2012-02-21 161 views
3

您知道iPhone在睡覺時如何播放鬧鐘聲, 就像iPhone中的內置時鐘應用程序一樣嗎?iPhone - 在後臺應用程序中播放鬧鐘聲音

非常重要的修改:在內置時鐘應用在iPhone
報警時播放聲音時,如果用戶切換靜音切換到靜音(振動模式),
報警聲仍繼續

玩。

你知道該怎麼做嗎?

+0

我已經在這個月了太多,檢查出[我的線程](http://stackoverflow.com/questions/ 9725192 /怎麼辦,我啓動扮演,音頻時,在靜音模式鎖定功能於IOS-6)。 – 2012-10-08 02:50:27

回答

0

只有一種方法是使用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

2

設定按鈕連接到運行在視圖控制器稱爲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

1

在應用程序進入後臺之前,使用AVAudioPlayer播放靜音並保持播放次數無限。當你的通知開始時,再次使用AVAudioPlayer播放你的通知聲音,而不是UILocalNotification對象。它爲我工作。

1

所以看看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

3

這個代碼將會幫助你在後臺播放音樂: 音樂的聲音文件應該只有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]; 
相關問題