简体   繁体   中英

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 .

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:

notification.soundName = [self playPause]; 

But I know this is a 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.

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];

}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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