尝试使用 UILocalNotification 播放音频

发布于 2025-01-05 21:42:18 字数 3127 浏览 1 评论 0原文

我的应用程序在后台状态时显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文