我的应用程序在处于后台状态时显示LocalNotification
。我想在LocalNotification
被解雇时播放声音。显然,我可以使用LocalNotification
。Soundname属性来提及应用程序包中的文件名。想要的是从文档目录播放声音,因为我在应用程序处于活动状态时录制声音并将其保存在文档目录中。我正在使用AVAudioPlayer
对象来播放它。它在模拟器中工作正常,但它不能在设备上工作。这是我如何做的代码。
if([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = [NSString stringWithFormat:@"You Got %d Email(s) From %@",newMailsFrom[i],[(ModelNameEmail *)[[MailModel sharedMailModel].EmailAlertArray objectAtIndex:i] Name]];
// Set the action button
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = +1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
localNotif.userInfo = infoDict;
//playing the sound
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *filename = [(ModelNameEmail *)[[MailModel sharedMailModel].EmailAlertArray objectAtIndex:0] Name];
NSString *filename1 = [filename stringByAppendingFormat:@".wav"];
NSString *filepath = [documentsDir stringByAppendingPathComponent:filename1];
NSLog(@"sound file path is %@",filepath);
//NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/welcome.wav", [[NSBundle mainBundle] resourcePath]]];
NSURL *url =[NSURL fileURLWithPath:filepath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer.numberOfLoops = 0;
[audioPlayer play];
// [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[localNotif release];
`