ios - 我如何使用 “App registers for location updates”背景模式在后台播放音频文件

标签 ios audio background location

我在我的应用程序中使用“位置更新的应用程序注册”后台模式来从后台获取位置信息。我的要求是,如果用户离预定位置更近(约100米),则需要播放音频文件。当应用程序处于 Activity 状态(前景)但未从后台播放任何音频时,此方法工作正常。

  self.locationManager = [[CLLocationManager alloc]init ];
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  self.locationManager.distanceFilter = kCLDistanceFilterNone;
  self.locationManager.delegate = self;
 [self.locationManager startUpdatingLocation];


 // delegate method
 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationDistance distance = [newLocation distanceFromLocation:definedLocation];
 //Check the distance between the newLocation and userDefinedLOcation
if (distance <=100.00) {
     NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.wav", [[NSBundle mainBundle] resourcePath],[[NSUserDefaults standardUserDefaults]valueForKey:@"alarmSound"]]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
player.numberOfLoops = -1;
player.currentTime = 0;
player.volume =1;
player.delegate=self;
[player prepareToPlay];
  [player play];

}
}

如何使用“用于位置更新的应用程序注册”后台模式在后台播放音频文件,帮助我实现这一目标。我的应用被拒绝了,因为我之前使用过“应用播放音频”背景模式。所以我不想使用音频背景模式。
任何帮助都适用。

最佳答案

您是否明确设置了音频 session 类别?

《音频 session 编程指南》有一个有用的部分“选择最佳类别”,它描述了不同的选项:
Audio Session Programming Guide

我现在不在Mac面前,因此无法测试。但是我怀疑将以下内容添加到您的viewDidLoad方法中可能会在您从应用程序执行多任务时帮助您。

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];

关于ios - 我如何使用 “App registers for location updates”背景模式在后台播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545619/

相关文章:

ios - 为什么 MFMailComposeViewController 返回 MFMailComposeResultFailed?

ios - Xcode 所有的 segues 显示都一样

audio - SimpleAudioEngine非异常断点

C# - 从文件夹中播放随机声音文件

android - libgdx 中的背景透明度

java - Java SWT 中的 ExpandBar/ExpandItem 背景/前景色

ios - 字符串与选项的比较在 iOS9.1 中崩溃,仅适用于 iPhone6 和 Xcode7

ios - 信标模拟器未检测到 iBeacon

python - 如何使用Python遍历第4行中的第4个像素?

css - 为 Web 背景上的图像叠加创建毛玻璃效果