iOS闹钟

时间:2013-07-29 11:09:18

标签: iphone ios audio avaudioplayer

我创建了一个简单的闹钟通知应用程序,通过它我可以获得实时,设置闹钟开启或关闭,以及播放单音音频。但我需要播放一个应该以课程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];

    }

}

我如何设置闹钟以开始播放radiosound如果被评为0,如:

notification.soundName = [self playPause]; 

但我知道这是NSString

1 个答案:

答案 0 :(得分:2)

您不需要为预定通知分配声音名称,只需调用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];

}