1

我在我的应用程序中使用“应用程序注册位置更新”后台模式从后台获取位置信息。我的要求是,如果用户更接近(大约 100 米)到预定义的位置,我需要播放音频文件。当应用程序处于活动状态(前台)但它没有从后台播放任何音频时,这工作正常。

  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];

}
}

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

4

1 回答 1

0

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

音频会话编程指南有一个有用的部分“选择最佳类别”描述了不同的选项: 音频会话编程指南

我现在不在 Mac 前,所以我无法对此进行测试。但我怀疑在您的 viewDidLoad 方法中添加以下内容可能会在您远离应用程序执行多任务时帮助您。

NSError *sessionError = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&sessionError];
于 2013-07-09T10:55:03.633 回答