简体   繁体   English

iOS闹钟

[英]iOS Alarm Clock

I have created a simple alarm notification App through which I can get real time, set alarm on or off, and play a single tone audio. 我创建了一个简单的警报通知应用程序,通过该应用程序,我可以获得实时,打开或关闭闹钟,以及播放单音音频。 But I need to play a sound which should start with a class VOID . 但是我需要播放一个应该以类VOID开头的声音。

Below is the code: 以下是代码:

To get and start alarm notification: 获取和启动警报通知:

- (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!"];
}

This is my VOID: 这是我的意见:

- (void)playPause {

    RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (appDelegate.radiosound == 0){

        [appDelegate.radiosound play];

    } else {

        [appDelegate.radiosound pause];

    }

}

How can I set the alarm to start playing the radiosound if is rated 0, like a: 如果评级为0,如何设置闹钟以开始播放radiosound ,如:

notification.soundName = [self playPause]; 

But I know this is a NSString . 但我知道这是一个NSString

You don't need to assign a sound name to scheduled notification, just invoke the playPause method and get the name of sound file from notification, as shown below and just assign it to NSString and set property to it in appDelegate and access it to play that file. 您不需要为预定通知分配声音名称,只需调用playPause方法并从通知中获取声音文件的名称,如下所示,只需将其分配给NSString并在appDelegate中将属性设置为它并访问它即可播放那个文件。

AppDelegate.h AppDelegate.h

@property (nonatomic,strong) NSString *nsStr_soundFile;

AppDelegate.m 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];

}

You can make a trick with opting out of iOS multitasking by setting in your app .plist file this key UIApplicationExitsOnSuspend to YES as written here 你可以一招用在您的应用程序设置选择退出的iOS多任务的.plist这个关键UIApplicationExitsOnSuspend文件到YES书面这里

When an app opts out, it cycles between the not-running, inactive, and active states and never enters the background or suspended states. 当应用程序选择退出时,它会在未运行状态,非活动状态和活动状态之间循环,并且永远不会进入后台或挂起状态。

An app that runs in the pre-multitasking compatibility mode keeps running when the user locks the device while the app is in the foreground. 当用户在应用程序位于前台时锁定设备时,以多任务前兼容模式运行的应用程序将继续运行。 All the app has to do is wait for the alarm time and execute its custom code. 应用程序所要做的就是等待闹钟时间并执行其自定义代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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