在UILocalNotification中如何为声音属性添加系统声音文件而不是自定义声音

时间:2014-11-03 22:12:46

标签: ios objective-c iphone xcode

我们可以添加到localNotification的自定义文件,但我的要求来自System/Library/Audio/UISounds/New/路径声音文件。需要根据用户选择进行播放

1 个答案:

答案 0 :(得分:-2)

非常简单:

我编辑了我的代码:

只需将路径添加为NSURL,然后将该网址添加到AVAudioPlayer。致电play方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/ma.mp3", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = -1;

 [audioPlayer play];


}

为uilocalnotification添加默认声音

- (void)scheduleNotification {

  [reminderText resignFirstResponder];
  [[UIApplication sharedApplication] cancelAllLocalNotifications];

  Class cls = NSClassFromString(@"UILocalNotification");
  if (cls != nil) {

    UILocalNotification *notif = [[cls alloc] init];
    notif.fireDate = [datePicker date];
    notif.timeZone = [NSTimeZone defaultTimeZone];

    notif.alertBody = @"Did you forget something?";
    notif.alertAction = @"Show me";

    notif.soundName = UILocalNotificationDefaultSoundName;
    notif.applicationIconBadgeNumber = 1;

    NSDictionary *userDict = [NSDictionary dictionaryWithObject:reminderText.text 
                              forKey:kRemindMeNotificationDataKey];
    notif.userInfo = userDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:notif];
    [notif release];
  }
}