3

我创建了一个简单的警报通知应用程序,通过它我可以实时获取警报,打开或关闭警报,并播放单音音频。但我需要播放一个应该以 class 开头的声音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:

- (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.

4

2 回答 2

2

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

}
于 2013-07-29T13:36:32.053 回答
0

您可以通过在您的应用程序 .plist 文件中将此键UIApplicationExitsOnSuspend设置为YES来选择退出 iOS 多任务处理,如此处所述

当应用程序选择退出时,它会在未运行、非活动和活动状态之间循环,并且永远不会进入后台或挂起状态。

当用户在应用程序处于前台时锁定设备时,在预多任务兼容模式下运行的应用程序会继续运行。该应用程序所要做的就是等待闹钟时间并执行其自定义代码。

于 2015-08-26T07:14:56.247 回答