繁体   English   中英

iOS闹钟

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM