[英]iOS Alarm Clock
我創建了一個簡單的警報通知應用程序,通過該應用程序,我可以獲得實時,打開或關閉鬧鍾,以及播放單音音頻。 但是我需要播放一個應該以類VOID
開頭的聲音。
以下是代碼:
獲取和啟動警報通知:
- (void)viewDidLoad {
[super viewDidLoad];
dateTimerPicker.date = [NSDate date];
}
- (void)presentMessage:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Hello!"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody = @"Time to wake up!!";
notification.soundName = @"PhoneOld.mp3";
[self playPause];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)alarmSetOn:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate:dateTimerPicker.date];
NSLog(@"Alarm Set: %@", dateTimeString);
[self scheduleLocalNotificationWithDate:dateTimerPicker.date];
[self presentMessage:@"Alarm ON!"];
}
- (IBAction)alarmSetOff:(id)sender {
NSLog(@"Alarm Off");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self presentMessage:@"Alarm OFF!"];
}
這是我的意見:
- (void)playPause {
RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.radiosound == 0){
[appDelegate.radiosound play];
} else {
[appDelegate.radiosound pause];
}
}
如果評級為0,如何設置鬧鍾以開始播放radiosound
,如:
notification.soundName = [self playPause];
但我知道這是一個NSString
。
您不需要為預定通知分配聲音名稱,只需調用playPause方法並從通知中獲取聲音文件的名稱,如下所示,只需將其分配給NSString
並在appDelegate中將屬性設置為它並訪問它即可播放那個文件。
AppDelegate.h
@property (nonatomic,strong) NSString *nsStr_soundFile;
AppDelegate.m
@synthesize nsStr_soundFile;
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
//Give call to play sound method.
self.nsStr_soundFile=notification.soundName;
VOID *obj=[VOID alloc]init];
[obj playPause];
}
你可以一招用在您的應用程序設置選擇退出的iOS多任務的.plist這個關鍵UIApplicationExitsOnSuspend文件到YES書面這里
當應用程序選擇退出時,它會在未運行狀態,非活動狀態和活動狀態之間循環,並且永遠不會進入后台或掛起狀態。
當用戶在應用程序位於前台時鎖定設備時,以多任務前兼容模式運行的應用程序將繼續運行。 應用程序所要做的就是等待鬧鍾時間並執行其自定義代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.