尝试使用 UILocalNotification 播放音频
我的应用程序在后台状态时显示 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];
`
My application shows a LocalNotification
when it is in background state. I want to play a sound when LocalNotification
is fired. Apparently I can use the LocalNotification
.Soundname property to mention the file name which is in the app bundle. What is want is to play a sound from documents directory,because I recorded sounds when the app is in active state and it is saved in documents directory. I am using the AVAudioPlayer
object to play it. Its working fine in the simulator, but it is not working on the device. Here is the code how I am doing it.
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];
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论